在android中使用两个适配器的一个活动

时间:2016-11-13 03:21:08

标签: android json android-recyclerview

我在Activity中使用RecyclerView和ExpandableListView,都将数据加载到一个布局,我试图将此活动调用用于一个适配器,但它似乎不成功。

这是我的活动(使用两个适配器):

public class DetailListCarActivityDemo extends AppCompatActivity {
  private RecyclerView mRecyclerView, exRecyclerView;
  private GridLayoutManager mLayoutManager;
  private RecyclerAdapter mAdapter;
  private ExpandableListAdapter exAdapter;
  private Context context;

private ArrayList<GetterSetter> feedItemList;
// private List<Item> itemsListData;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail_list_car_demo);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsingToolbar.setTitle("Honda CRV Test");

    //find recyclerview layout
    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    exRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);

    mRecyclerView.setHasFixedSize(true);

    exRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

    // recyclerview set layoutmanager
    mLayoutManager = new GridLayoutManager(this,3);
    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return (3 - position % 3);
        }
    });
    // RecyclerView.LayoutManager lm = new GridLayoutManager(this,2);

    mRecyclerView.setLayoutManager(mLayoutManager);

    //adding data to arraylist
    feedItemList = new ArrayList<GetterSetter>();
    for (int i = 0; i < 7; i++) {
        GetterSetter getterSetter = new GetterSetter();
        feedItemList.add(getterSetter);
    }

    //recyclerview adapter
    mAdapter = new RecyclerAdapter(DetailListCarActivityDemo.this, feedItemList);
    //set adpater for recyclerview


    List<ExpandableListAdapter.Item> data = new ArrayList<>();

    data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.HEADER, "Fruits"));
    data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Apple"));
    data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Orange"));
    data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Banana"));
    data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.HEADER, "Cars"));
    data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Audi"));
    data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Aston Martin"));
    data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "BMW"));
    data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Cadillac"));

    ExpandableListAdapter.Item places = new ExpandableListAdapter.Item(ExpandableListAdapter.HEADER, "Places");
    places.invisibleChildren = new ArrayList<>();
    places.invisibleChildren.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Kerala"));
    places.invisibleChildren.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Tamil Nadu"));
    places.invisibleChildren.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Karnataka"));
    places.invisibleChildren.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Maharashtra"));

    data.add(places);

    exRecyclerView.setAdapter(new ExpandableListAdapter(data));

    mRecyclerView.setAdapter(mAdapter);
  }
 }

我的布局:

<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:fitsSystemWindows="true">

<!--
<android.support.v7.widget.RecyclerView
    android:id="@+id/cardList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
-->

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/mercedes"
            android:contentDescription="@string/paralax"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    app:layout_anchor="@+id/appbar"
    app:layout_anchorGravity="bottom|right|end" />

我想问我可以通过两个适配器并将数据加载到一个布局吗?

0 个答案:

没有答案