2017-09-14 75 views
0

我创建标签活动有两个选项卡(的EditText在TAB1,列表视图中TAB2),和我通过TAB1数据TAB2到ListView控件,如何设置标题列表视图集标题列表视图选项卡式活动

请帮助

我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="store.exercise.com.store.MainActivity$PlaceholderFragment"> 

<ListView 
    android:id="@+id/list_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</RelativeLayout> 

片段之一:

public class FragmentOne extends Fragment { 

SendMessage SM; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View rootView = inflater.inflate(
      R.layout.fragment_one, container, false); 
    return rootView; 


} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    Button btnPassData = (Button) view.findViewById(R.id.btnPassData); 
    final EditText inData = (EditText) view.findViewById(R.id.inMessage); 
    btnPassData.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      SM.sendData(inData.getText().toString().trim()); 
     } 
    }); 

} 

interface SendMessage { 
    void sendData(String message); 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 

    try { 
     SM = (SendMessage) getActivity(); 
    } catch (ClassCastException e) { 
     throw new ClassCastException("Error in retrieving data. Please try again"); 
    } 
} 
} 

片段TW ○:

public class FragmentTwo extends Fragment { 

ListView listView; 
ArrayList<String> arrayList = new ArrayList<>(); 
ArrayAdapter<String> adapter; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View rootView = inflater.inflate(
      R.layout.fragment_two, container, false); 
    return rootView; 


} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    listView = (ListView) view.findViewById(R.id.list_view); 
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList); 

    listView.setAdapter(adapter); 
} 

protected void displayReceivedData(String message) { 
    arrayList.add(message); 
    adapter.notifyDataSetChanged(); 

} 
} 

我ViewPagerAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter { 
    public ViewPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     Fragment fragment = null; 
     if (position == 0) { 
      fragment = new FragmentOne(); 
     } else if (position == 1) { 
      fragment = new FragmentTwo(); 
     } 
     return fragment; 
    } 

    @Override 
    public int getCount() { 
     return 2; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     String title = null; 
     if (position == 0) { 
      title = " مهمة جديدة "; 
     } else if (position == 1) { 
      title = " المهام "; 
     } 
     return title; 
    } 
} 
+0

您的标题是什么意思?把标题放在哪里?在listview上的标题?我没有得到它,也许你问工具栏/ actionBar的标题? –

+0

listview的标题 –

+0

是否可以为托管标签视图的活动添加xml代码 –

回答

0

确保你有你的activity_view.xml,你有你的viewpager以下

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/center_section_tab_bar_header_height" 
       app:tabGravity="fill" 
       app:tabMode="fixed" /> 
     </android.support.design.widget.AppBarLayout> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
    </android.support.design.widget.CoordinatorLayout> 

,并在活动课中添加以下行使用标签布局标题设置视图打印机

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(yourviewpager); 

之后它会为您的代码设置标题,如代码中提到的