在自定义视图中嵌入RecyclerView

时间:2016-07-10 10:33:33

标签: java android android-recyclerview android-custom-view

我正在尝试在自定义视图(RecyclerView)的正文中嵌入RefreshableList

这是我项目的结构:它包含2个模块, app &的 RLIST 即可。

Project view

rlist 模块包含我要嵌入RefreshableList的自定义视图(RecyclerView)。

RefreshableList.java (自定义视图)

public class RefreshableList extends RelativeLayout {

    private RecyclerView mRecyclerView;

    private Context mContext;
    private MyAdapter mAdapter;

    public RefreshableList(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        Log.i("ADAPTER", "RefreshableList is initializing views...");

        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.refreshable_list, null);

        findViewsById(view);
        setupRecyclerView();
    }

    private void findViewsById(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.dataRecyclerView);
        Log.i("ADAPTER", "RecyclerView has been initialized.");
    }

    private void setupRecyclerView() {
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        Log.i("ADAPTER", "RecyclerView has been setup.");
    }

    public void setAdapter(MyAdapter adapter) {
        mAdapter = adapter;
        mRecyclerView.setAdapter(mAdapter);
        Log.i("ADAPTER", "Adapter has been set." + mAdapter.getItemCount());
    }
}

MyAdapter.java RecyclerView的适配器)

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    protected Context mContext;
    protected List<String> mItems;

    public MyAdapter(Context context, List<String> items) {
        mContext = context;
        mItems = new ArrayList<>(items);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_row, parent, false);
        Log.i("ADAPTER", "Creating row...");

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.label.setText(mItems.get(position));
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView label;

        public ViewHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.label);
        }
    }
}

item_row.xml:RecyclerView元素的XML布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="60dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="New Text"
        android:id="@+id/label"
        android:gravity="left|center_vertical"
        android:padding="8dp"
        android:background="#ff0000"/>
</LinearLayout>

refreshable_list.xml:RefreshableList自定义视图的XML布局)

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/dataRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

所有这些代码都属于 rlist 模块。为了对其进行测试,我在应用模块中添加了MainActivity,我在其中嵌入了RefreshableList

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.list)
    RefreshableList mRefreshableList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        mRefreshableList.setAdapter(new MyAdapter(
                this, Arrays.asList("Java", "Android", "Python", "Kivy")));
    }
}

activity_main.xml中

<?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="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zouag.refreshablelist.MainActivity"
    android:background="#00ff00">

    <com.zouag.rlist.RefreshableList
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffff00"/>
</RelativeLayout>

运行应用程序后,我看不到任何RecyclerView的元素:(即使RecyclerView清晰可见,标有黄色)

Run

我在这里缺少什么?

1 个答案:

答案 0 :(得分:3)

不是您的recyclerview可见,您的相对布局是。 您没有将膨胀的布局附加到您的视图组。

电话:

View view = inflater.inflate(R.layout.refreshable_list, null);

只需夸大布局,但不要将其附加到视图中。 如果你想坚持下去,你需要在充气后附上视图:

this.addView(view)

或者只是打电话:

View view = inflater.inflate(R.layout.refreshable_list, this,true);

将膨胀的布局附加到根视图。