Recyclerview不显示商品

时间:2017-09-28 03:12:58

标签: android android-recyclerview recycler-adapter recyclerview-layout

在将数据集设置到适配器之前,我从Room数据库获取数据集,然后将数据设置为适配器并将适配器设置为recyclerview。目前列表中有2个项目。从我的日志中我看到所有方法都被调用列表中的第一项而不是第二项,并且没有项目或视图显示在屏幕上。我不确定发生了什么事有人能帮助我发现问题吗?在此先感谢您的帮助。

goals_content.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:background="#ffffff"
    android:clickable="true"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/goals_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="LinearLayoutManager"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1px"
        android:visibility="gone"
        />

    <!--view to show if the dataset is empty-->
    <TextView
        android:id="@+id/emptyElement"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:visibility="visible"
        android:text="No Goals Added"
        android:textColor="#525252"
        android:textSize="19sp"/>
</LinearLayout>

GoalsActivity.java

GoalsRecyclerViewAdapter adapter;
RecyclerView goalsListView;
....

goalsListView = (RecyclerView) findViewById(R.id.goals_list);

....

private void createGoalsList() {

        new AsyncTask<Void, Void, List<Goal>>(){

            @Override
            protected List<Goal> doInBackground(Void... params) {
                List<Goal> returnedGoals = goalDao.getAllGoals();
                return returnedGoals;
            }

            @Override
            protected void onPostExecute(List<Goal> returnedGoals) {

                //check your goals list is empty or not
                if(returnedGoals.size() > 0){

                    //make the goals list visible if there is data
                    goalsListView.setVisibility(View.VISIBLE);
                    noGoalsTxt.setVisibility(View.GONE);

                    //set array adapter for the goals list
                    adapter = new GoalsRecyclerViewAdapter(returnedGoals);
                    goalsListView.setAdapter(adapter);
                }else{

                    //if there is no data display empty list text.
                    goalsListView.setVisibility(View.GONE);
                    noGoalsTxt.setVisibility(View.VISIBLE);
                }
            }
        }.execute();
    }

GoalsRecyclerviewAdapter.java

public class GoalsRecyclerViewAdapter extends RecyclerView.Adapter<GoalsRecyclerViewAdapter.GoalsViewHolder> {

    private static final String TAG = GoalsRecyclerViewAdapter.class.getSimpleName();

    private List<Goal> mGoals;

    public GoalsRecyclerViewAdapter(List<Goal> goals) {
        Log.d(TAG, "adapter initialized");
        mGoals = goals;
    }

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

        Log.d(TAG, "onCreateViewHolder: inside");
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goal_row, parent, false);
        return new GoalsViewHolder(view);
    }

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

        Log.d(TAG, "onBindViewHolder: position " + position);
        holder.bind(mGoals.get(position));
    }

    @Override
    public int getItemCount() {
        Log.d(TAG, "getItemCount: " + mGoals.size());
        return mGoals.size();
    }

    class GoalsViewHolder extends RecyclerView.ViewHolder {

        TextView goalsLabel, goalCount;

        GoalsViewHolder(View itemView) {
            super(itemView);
            goalsLabel = (TextView) itemView.findViewById(R.id.calls_goal_label);
            goalCount = (TextView) itemView.findViewById(R.id.goal_count);
        }

        void bind(Goal goal){
            Log.d(TAG, "bind: " + goal.toString());
            goalsLabel.setText(goal.getGoalTitle());
            goalCount.setText(String.valueOf(goal.getGoalTarget()));
        }
    }

    public List<Goal> getGoals(){
        return mGoals;
    }

    public void setGoals(List<Goal> goals){
        mGoals = goals;
    }
}

更新:添加了行布局 goal_row.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.ubcma.leadster.activity.GoalsActivity"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:id="@+id/calls_goal_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="23sp"
        android:layout_alignParentLeft="true"
        android:layout_margin="@dimen/text_margin"
        tools:text="@string/calls_per_week"/>

    <TextView
        android:id="@+id/goal_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_margin="@dimen/text_margin"
        android:textSize="23sp"
        tools:text="4"/>

</RelativeLayout>

日志

09-27 23:10:08.071 1606-1818/? D/ActivityManager: resumeTopActivityInnerLocked() : #1 prevTask=TaskRecord{46a33bdd0 #6227 A=com.ubcma.leadster U=0 StackId=1 sz=2} next=ActivityRecord{8396555d0 u0 com.ubcma.leadster/.activity.GoalsActivity t6227} mFocusedStack=ActivityStack{43432a4d0 stackId=1, 38 tasks}
09-27 23:10:08.128 24667-24667/com.ubcma.leadster D/ViewRootImpl@14dce6f[GoalsActivity]: setView = DecorView@be0e77c[GoalsActivity] touchMode=true
09-27 23:10:08.211 1606-4253/? D/WindowManager: finishDrawingWindow: Window{94ca436d0 u0 com.ubcma.leadster/com.ubcma.leadster.activity.GoalsActivity} mDrawState=DRAW_PENDING
09-27 23:10:08.213 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: adapter initialized
09-27 23:10:08.214 24667-24667/com.ubcma.leadster D/ViewRootImpl@14dce6f[GoalsActivity]: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
09-27 23:10:08.214 24667-24667/com.ubcma.leadster D/ViewRootImpl@14dce6f[GoalsActivity]: MSG_WINDOW_FOCUS_CHANGED 1
09-27 23:10:08.225 1606-1917/? I/ActivityManager: Displayed com.ubcma.leadster/.activity.GoalsActivity: +153ms
09-27 23:10:08.229 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: getItemCount: 2
09-27 23:10:08.229 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: getItemCount: 2
09-27 23:10:08.230 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: getItemCount: 2
09-27 23:10:08.230 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: onCreateViewHolder: inside
09-27 23:10:08.233 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: onBindViewHolder: position 0
09-27 23:10:08.234 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: bind: Goal{id=1, goalType='r', goalTitle='Recruit', goalFrequency='Month', goalTarget=12}
09-27 23:10:08.247 1606-12625/? D/WindowManager: finishDrawingWindow: Window{94ca436d0 u0 com.ubcma.leadster/com.ubcma.leadster.activity.GoalsActivity} mDrawState=HAS_DRAWN

4 个答案:

答案 0 :(得分:3)

您需要在recyclelerview中设置适配器之前指定布局管理器,如下所示

AudioRecord

或在布局中指定,如下所示

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);

答案 1 :(得分:1)

android:visibility="visible" xml代码

中设置RecyclerView

更改

<android.support.v7.widget.RecyclerView
    android:id="@+id/goals_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="LinearLayoutManager"
    android:divider="@android:color/darker_gray"
    android:dividerHeight="1px"
    android:visibility="gone"
    />

<android.support.v7.widget.RecyclerView
    android:id="@+id/goals_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="LinearLayoutManager"
    android:divider="@android:color/darker_gray"
    android:dividerHeight="1px"
    android:visibility="visible"
    />

修改

将layoutManager添加到代码中。

goalsListView.setLayoutManager(new LinearLayoutManager(this));

并删除xml代码中的app:layoutManager="LinearLayoutManager"

答案 2 :(得分:0)

保持回收商项目的高度为match_parent,每个屏幕只显示一个视图。将您的回收商项目高度从match_parent更改为wrap_content。

同时删除app:layout_behavior =“@ string / appbar_scrolling_view_behavior”。

检查此更新代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/calls_goal_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="23sp"
    android:layout_alignParentLeft="true"
    android:layout_margin="20dp"
    android:text="sample"
/>

<TextView
    android:id="@+id/goal_count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_margin="20dp"
    android:textSize="23sp"
    tools:text="4"/>

答案 3 :(得分:0)

LayoutManager RecyclerView 中最有趣的部分。这个对象告诉 RecyclerView 何时回收已经过渡到屏幕外的对象以及它们可以去哪里。这以前仅由 ListView 完成。此功能已与 RecyclerView 分离,以允许多种布局:垂直、水平、网格、分阶段或您自己的布局!

LinearLayoutManager 以类似于常规 ListView 的方式排列对象。

GridLayoutManager 与 GridView 类似,以网格样式排列对象。

StaggeredGridLayoutManager 以交错的网格格式排列项目。

您可以添加如下所示的 layoutmanager

科特林

var linearLayoutManager = LinearLayoutManager(this)
recycler.layoutManager = linearLayoutManager

Java

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recycler.setLayoutManager(linearLayoutManager);