我想在片段內使用ListView
,在頂部有可滑動的選項卡。這些標籤與ListView
重疊,並且當我嘗試在標籤之間滑動時,該應用也會崩潰。ListView重疊TabLayout和按鈕在片段中使用
這是它的外觀:
我一直在試圖整天尋找一個解決方案,但還沒有找到一個適合我的工作。既然這是我的第一個問題在這裏,我真的想盡可能地理解我想要的東西(如果您需要其他任何東西,該項目在Git here上)。
我用CoordinatorLayout
對於要膨脹的片段,但我讀了ListView
和佈局沒有能夠很好地配合,所以現在我只是想獲得它與RelativeLayout
或LinearLayout
工作。我也嘗試過使用RecyclerView
,但那也沒用。
我也試過在LinearLayout
和RelativeLayout
之間任何地方都可以改變。所以現在我想聽聽有人能幫助我。以下是代碼現在的樣子。
activity_main.xml中 -where標籤和viewpager有自己的看法
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</LinearLayout>
workout_list_layout.xml -where listlayout設置
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1.0">
</ListView>
</RelativeLayout>
FRA gment_workout.xml -a片段應該顯示的分頁
<RelativeLayout
android:id="@+id/main_list"
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:orientation="vertical">
<LinearLayout
android:layout_below="@id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/workoutList">
<include layout="@layout/workout_list_layout"
android:id="@+id/list"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add_workout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_input_add"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_above="@id/workoutList"
/>
<Button
android:id="@+id/button_start_workout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_gravity="bottom|left"
android:text="Start"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_above="@id/workoutList"/>
</RelativeLayout>
PageFragment.java 下面的列表-handles所有片段
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view;
if(mPage == 1){
view = inflater.inflate(R.layout.fragment_workout, container, false);
WorkoutListFragment workoutListFragment = (WorkoutListFragment)getFragmentManager().findFragmentByTag("workoutlistfragment");
if (workoutListFragment == null){
workoutListFragment = new WorkoutListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(android.R.id.content, workoutListFragment,"workoutlistfragment");
transaction.commit();
}
Button button = (Button) view.findViewById(R.id.button_start_workout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_start_workout:
Intent intent = new Intent(view.getContext(), TimerWindow.class);
startActivity(intent);
break;
}
}
});
}else if(mPage == 2){
view = inflater.inflate(R.layout.fragment_performance, container, false);
}else{
view = inflater.inflate(R.layout.fragment_options, container, false);
}
return view;
}
}
WorkoutListFragment.java -listfragment膨脹視圖
public class WorkoutListFragment extends ListFragment {
ArrayList<String> listItems = new ArrayList<String>(); //List items
ArrayAdapter<String> adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.workout_list_layout, container, false);
return rootView;
}
public void onListItemClick(ListView listView, View view, int position, long id){
ViewGroup viewGroup = (ViewGroup) view;
TextView textView = (TextView) viewGroup.findViewById(R.id.textItem);
Toast.makeText(getActivity(), textView.getText().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" , "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(String workoutTitle, View v) {
listItems.add(workoutTitle);
adapter.notifyDataSetChanged();
}
}
謝謝!嘗試它,它不工作... 它現在是這樣的: https://drive.google.com/open?id=0B5J-MYgiEYRwNHVSR1VhY2hKR1k 任何其他的想法? –