2016-05-17 5 views
3

フラグメント内にListViewを使用したい場合、上部にスワイプ可能なタブがあります。これらのタブはListViewで重複しており、タブ間でスワイプするとアプリがクラッシュします。リストビューのオーバーラップTabLayoutとボタンをフラグメント内で使用すると

これは、それがどのように見えるかです:私は解決策を検索するためのすべての日をしようとしてきたが、私のために働いていたものを発見していない

Screenshot_overlap

...それは私の最初の質問ですので、ここでは、私が望むものとできるだけ理解できるようにしようとしています(そして、何か他のものが必要な場合は、Git hereにあります)。

私は膨らませる必要があるフラグメントのためCoordinatorLayoutを使用しますが、私はListViewとそのレイアウトは一緒にうまくいかなかったので、今、私はちょうどそれがRelativeLayoutまたはLinearLayoutで動作するように取得したいことをお読みください。私もRecyclerViewを使ってみましたが、うまくいきませんでした。

LinearLayoutRelativeLayoutの間で変更を試みました。だから私は誰かが私を助けることができるかどうか聞いてみたかった。以下は、コードの現在の見方です。

activity_main.xml -whereタブとviewpagerは、自分の意見 -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に設定されている

<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を有しますgment_workout.xml タブの下のリストに

<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> 

を示すべき-aフラグメントPageFragment.javaは、すべてのフラグメント

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 を-handles - ビューを膨らませるリストフラグメント

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は実際にあなたのレイアウトの一部ではありません。代わりに、あなたはこのようなあなたの主な活動のXMLでは、あなたのタブの下とあなたのviewpager上、お使いのフラグメントのプレースホルダレイアウトを作成する必要があります。

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

私はそれが動作するかどうかをお知らせください。

関連する問題