繁体   English   中英

Android- RecyclerView:未附加适配器的跳过布局

[英]Android- RecyclerView : No Adapter Attached Skipping Layout

我有一个带有tabLayoutActivity ,每个选项卡都由一个Fragment表示(我有三个片段:Home,Caegories和我的帐户),所以我想在HomeFragment上显示水平RecyclerView ,一切正常,但RecyclerView无效,我得到这个错误:

E / RecyclerView:未连接适配器; 跳过布局。

我已经测试过此解决方案,但是我什么也没得到。

这是我做的:

首先: ActivityMain和xml布局:

    private ArrayList<OfferItem> dataList;
    private RecyclerView myRecycler;
    private View fragmentViewLayout;
    private LastOffersRecyclerAdapter listAdapter;
   //......
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setViewPager();
            setTabIcons();

            mTabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
            mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {

                    int tabPoaition = tab.getPosition();

                    if(tabPoaition == 0){
                        View view = tab.getCustomView();
                        TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
                        tabTextView.setTextColor(Color.parseColor("#ef4437"));
                        tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_home_red, 0, 0);
                    }
                    else if(tabPoaition ==1){
                        View view = tab.getCustomView();
                        TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
                        tabTextView.setTextColor(Color.parseColor("#ef4437"));
                        tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_category_red, 0, 0);
                    }
                    else{
                        View view = tab.getCustomView();
                        TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
                        tabTextView.setTextColor(Color.parseColor("#ef4437"));
                        tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_person_red, 0, 0);
                    }

                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                    int tabPoaition = tab.getPosition();

                    if(tabPoaition == 0){
                        View view = tab.getCustomView();
                        TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
                        tabTextView.setTextColor(Color.parseColor("#bdbcbc"));
                        tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_home_gray, 0, 0);
                    }
                    else if(tabPoaition ==1){
                        View view = tab.getCustomView();
                        TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
                        tabTextView.setTextColor(Color.parseColor("#bdbcbc"));
                        tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_category_gray, 0, 0);
                    }
                    else{
                        View view = tab.getCustomView();
                        TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
                        tabTextView.setTextColor(Color.parseColor("#bdbcbc"));
                        tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_person_gray, 0, 0);
                    }
                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });

            fragmentViewLayout = getLayoutInflater().inflate(R.layout.fragment_one, null);
            listAdapter = new LastOffersRecyclerAdapter(this,dataList);

            myRecycler = (RecyclerView)fragmentViewLayout.findViewById(R.id.my_list_recycler);
            myRecycler.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
            myRecycler.setItemAnimator(new DefaultItemAnimator());
            myRecycler.setAdapter(listAdapter);

            createSampleData();
        }
     ///...
    private void createSampleData(){
            dataList = new ArrayList<OfferItem>();
            OfferItem itm;

            for(int i =0 ;i<10 ; i++){
                itm = new OfferItem(i,"business_logo.png","offer_img.png","La Piscope","Fes");
                dataList.add(itm);
            }

            listAdapter.notifyDataSetChanged();
        }

activity_main.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.biliki.biliki.bilikiapp.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/my_home_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.TabLayout
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed"
            app:tabBackground="?attr/selectableItemBackground"
            />

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

    <include
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:layout_constraintTop_toBottomOf="@id/my_home_app_bar"
        app:layout_constraintLeft_toLeftOf="parent"
        layout="@layout/content_main" />

</android.support.constraint.ConstraintLayout>

第二:我的AdapterClass LastOffersRecylerAdapter.java

public class LastOffersRecyclerAdapter extends RecyclerView.Adapter<LastOffersRecyclerAdapter.OffersItemHolder> {

    private Context context;
    private ArrayList<OfferItem> dataList;

    public LastOffersRecyclerAdapter(Context context, ArrayList<OfferItem> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public OffersItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.last_offers_item,parent,false);
        OffersItemHolder offHolder = new OffersItemHolder(v);
        return offHolder;
    }

    @Override
    public void onBindViewHolder(OffersItemHolder holder, int position) {

        OfferItem item = dataList.get(position);
        int logoResourceId = context.getResources().getIdentifier(item.getLogoUrl(), "drawable", context.getPackageName());
        int offerImgResourceId = context.getResources().getIdentifier(item.getOfferImgUrl(), "drawable", context.getPackageName());

        holder.busniessLogo.setImageResource(logoResourceId);
        holder.offerImg.setImageResource(offerImgResourceId);
        holder.offer_business_city.setText(item.getBusiness_name() + "| "+item.getBusiness_city());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


    public class OffersItemHolder extends RecyclerView.ViewHolder{

        public ImageView offerImg;
        public ImageView busniessLogo;
        public TextView offer_business_city;


        public OffersItemHolder(View itemView) {
            super(itemView);
            this.offerImg = itemView.findViewById(R.id.offer_img);
            this.busniessLogo  = itemView.findViewById(R.id.business_logo);
            this.offer_business_city = itemView.findViewById(R.id.business_name_city);
        }
    }

这是HomeFragment的xml布局

<android.support.constraint.ConstraintLayout 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:padding="5dp">

    <RelativeLayout
        android:id="@+id/title_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <com.biliki.biliki.bilikiapp.uitemplate.font.RobotoTextView
            android:id="@+id/itemTitle"
            android:paddingLeft="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:layout_toLeftOf="@+id/btnMore"
            android:text="@string/lastoffersTitle" />

        <Button
            android:id="@+id/btnMore"
            style="@style/allButtonsstyle"
            android:layout_width="65dp"
            android:layout_height="42dp"
            android:textAlignment="gravity"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="@string/moreLastOffers" />
    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_list_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title_layout" />

</android.support.constraint.ConstraintLayout>

将显示TextView和按钮,但不会显示RecyclerView 您能解决这个问题吗? 向你致敬。

更新:

content_main xml文件包含tabLayout ViewPager

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="@layout/activity_main">

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabs_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

正如Mike M先生提到的,我已将代码移到HomeFragment类,并且它可以工作。

这是HomeFragment类:

public class HomeFragment extends Fragment {

    public static final String TITLE = "Accueil";
    private RecyclerView myRecycler;
    private LastOffersRecyclerAdapter listAdapter;
    private ArrayList<OfferItem> dataList;

    public static HomeFragment newInstance() {
        return new HomeFragment ();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        createSampleData();
        View rootView = inflater.inflate(R.layout.fragment_one, container, false);
        listAdapter = new LastOffersRecyclerAdapter(getContext(),dataList);
        myRecycler  = rootView.findViewById(R.id.my_list_recycler);
        myRecycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        myRecycler.setItemAnimator(new DefaultItemAnimator());
        myRecycler.setAdapter(listAdapter);



        return  rootView;
    }

    private void createSampleData(){
        dataList = new ArrayList<OfferItem>();
        OfferItem itm;

        for(int i =0 ;i<10 ; i++){
            itm = new OfferItem(i,"business_logo.png","offer_img.png","La Piscope","Fes");
            dataList.add(itm);
        }

    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM