2016-05-17 36 views
3

我想在片段內使用ListView,在頂部有可滑動的選項卡。這些標籤與ListView重疊,並且當我嘗試在標籤之間滑動時,該應用也會崩潰。ListView重疊TabLayout和按鈕在片段中使用

這是它的外觀:

Screenshot_overlap

我一直在試圖整天尋找一個解決方案,但還沒有找到一個適合我的工作。既然這是我的第一個問題在這裏,我真的想盡可能地理解我想要的東西(如果您需要其他任何東西,該項目在Git here上)。

我用CoordinatorLayout對於要膨脹的片段,但我讀了ListView和佈局沒有能夠很好地配合,所以現在我只是想獲得它與RelativeLayoutLinearLayout工作。我也嘗試過使用RecyclerView,但那也沒用。

我也試過在LinearLayoutRelativeLayout之間任何地方都可以改變。所以現在我想聽聽有人能幫助我。以下是代碼現在的樣子。

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(); 
} 
} 

回答

0

據我所知,您必須處理您的佈局文件以正確排序您的視圖。在「activity_main.xml」中通過「android:layout_below」屬性在「TabLayout」下面設置「ViewPager」。同樣在「fragment_workout.xml」中,在同一文件中將LinearLayout的「android:layout_height」屬性值更改爲「wrap_content」,將「button_start_workout」屬性「android:layout_above」更改爲「android:layout_below」。這應該做到這一點。希望這可以幫助。

+0

謝謝!嘗試它,它不工作... 它現在是這樣的: https://drive.google.com/open?id=0B5J-MYgiEYRwNHVSR1VhY2hKR1k 任何其他的想法? –

1

我可以看到你試圖用你的片段替換你的「android.R.id.content」。但是android.R.id.content並不是你佈局的一部分。相反,你應該爲你的片段佔位佈局,在你的標籤,並在你的viewpager,在主要活動這樣的XML:

<LinearLayout 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="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" /> 

    <FrameLayout 
     android:id="@+id/fragmentContainer" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/white" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</LinearLayout> 

然後,在你PageFragment.java,更換(不添加)您佔位符與您的片段,如下所示:

WorkoutListFragment workoutListFragment = (WorkoutListFragment) getFragmentManager().findFragmentByTag("workoutlistfragment"); 
      if (workoutListFragment == null) { 
       workoutListFragment = new WorkoutListFragment(); 
       FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
       transaction.replace(R.id.fragmentContainer, workoutListFragment).commit(); 
      } 

讓我知道,如果這樣的作品。