LinearLayout中的第三个RecyclerView将不会显示

时间:2018-10-19 15:33:34

标签: android android-recyclerview android-linearlayout

我在LinearLayout中有3个RecyclerViews。我的目标是依次显示所有这些对象,因此第一个RecylerView位于顶部,第二个RecyclerView位于中间,第三个RecyclerView位于底部。 但是第三个RecyclerView不会出现在屏幕上。在下面,您可以看到我的文件:

// GroupsScreenActivity.java
public class GroupsScreenActivity extends AppCompatActivity {

    private RecyclerView recyclerViewGoldenGroup;
    private RecyclerView recyclerViewSilverGroup;
    private RecyclerView recyclerViewBronceGroup;

    private GoldenRecyclerViewAdapter goldenGroupRecyclerViewAdapter;
    private SilverGroupRecyclerViewAdapter silverGroupRecyclerViewAdapter;
    private BronceRecyclerViewAdapter bronceGroupRecyclerViewAdapter;


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

        // get a reference to the golden recyclerView
        recyclerViewGoldenGroup = (RecyclerView) findViewById(R.id.rvGoldenGroup);

        // create the adapter
        goldenGroupRecyclerViewAdapter = new GoldenRecyclerViewAdapter(getSampleArrayList());

        // create the LinearLayoutManager
        LinearLayoutManager goldenLayoutManager =  new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);

        // set the LayoutManager
        recyclerViewGoldenGroup.setLayoutManager(goldenLayoutManager);

        // set the adapter
        recyclerViewGoldenGroup.setAdapter(goldenGroupRecyclerViewAdapter);
        ///////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////

        // get a reference to the silver RecyclerView
        recyclerViewSilverGroup = (RecyclerView) findViewById(R.id.rvSilverGroup);

        // create the adapter
        silverGroupRecyclerViewAdapter = new SilverGroupRecyclerViewAdapter(getSampleArrayList());

        // create the LinearLayoutManager
        LinearLayoutManager silverLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);

        // set the LayoutManager
        recyclerViewSilverGroup.setLayoutManager(silverLayoutManager);

        // set the adapter
        recyclerViewSilverGroup.setAdapter(silverGroupRecyclerViewAdapter);
        ////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////

        // get the reference to the bronce RecyclerView
        recyclerViewBronceGroup = (RecyclerView) findViewById(R.id.rvBronceGroup);

        // create the adapter
        bronceGroupRecyclerViewAdapter = new BronceRecyclerViewAdapter(getSampleArrayList());

        // create the LayoutManager
        LinearLayoutManager bronceLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);

        // set the LayoutManager
        recyclerViewBronceGroup.setLayoutManager(bronceLayoutManager);

        // set the adapter
        recyclerViewSilverGroup.setAdapter(bronceGroupRecyclerViewAdapter);

    }

    private ArrayList<Object> getSampleArrayList() {
        ArrayList<Object> items = new ArrayList<>();
        items.add(new User("Dany Targaryen", "Valyria"));
        items.add(new User("Rob Stark", "Winterfell"));
        items.add("image");
        items.add(new User("Jon Snow", "Castle Black"));
        items.add("image");
        items.add(new User("Tyrion Lanister", "King's Landing"));
        return items;
    }
}

在下面,您可以看到布局文件。这由一个带有垂直LinearLayout的ConstraintLayout组成,它具有3个RecyclerViews作为其子级:

<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=".GroupsScreen">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvGoldenGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvSilverGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvBronceGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

为简洁起见,我仅添加顶部RecyclerView的RecyclerView.Adapter子类。其他两个完全相同:

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

    // The items to display in your RecyclerView
    private List<Object> items;

    private final int USER = 0, IMAGE = 1;

    // Provide a suitable constructor (depends on the kind of dataset)
    public GoldenRecyclerViewAdapter(List<Object> items) {
        this.items = items;
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return this.items.size();
    }

    // we need to override this method to tell the
    // "RecyclerView" about the type of view to inflate
    // based on the position
    // we'll return USER or IMAGE based on the type object
    // in the data we have
    @Override
    public int getItemViewType(int position) {

        if(items.get(position) instanceof User){
            return USER;
        }
        else if(items.get(position) instanceof String){
            return IMAGE;
        }

        return -1;
    }

    // we need to override this method
    // to tell the "RecyclerView.Adapter" about which "RecyclerView.ViewHolder"
    // object to create based on the "viewType" returned
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder viewHolder;

        LayoutInflater inflater = LayoutInflater
                .from(viewGroup.getContext());

        switch (viewType){
            case USER:
                View v1 = inflater.inflate(
                        R.layout.layout_viewholder1,
                        viewGroup,
                        false);
                viewHolder = new ViewHolder1(v1);
                break;
            case IMAGE:
                View v2 = inflater.inflate(
                        R.layout.layout_viewholder2,
                        viewGroup,
                        false);
                viewHolder = new ViewHolder2(v2);
                break;
            default:
                View view = inflater.inflate(
                        android.R.layout.simple_list_item_1,
                        viewGroup,
                        false);
                viewHolder = new RecyclerViewSimpleTextViewHolder(view);
                break;

        }
        return viewHolder;
    }

    // we override this method
    // to configure the "ViewHolder" with actual data that needs to be
    // displayed
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        switch (viewHolder.getItemViewType()) {
            case USER:
                ViewHolder1 vh1 = (ViewHolder1) viewHolder;
                configureViewHolder1(vh1, position);
                break;
            case IMAGE:
                ViewHolder2 vh2 = (ViewHolder2) viewHolder;
                configureViewHolder2(vh2);
                break;

            default:
                RecyclerViewSimpleTextViewHolder vh = (RecyclerViewSimpleTextViewHolder) viewHolder;
                configureDefaultViewHolder(vh, position);
                break;

        }
    }

    private void configureDefaultViewHolder(RecyclerViewSimpleTextViewHolder vh, int position) {
        vh.getTv().setText((CharSequence) items.get(position));
    }


    private void configureViewHolder1(ViewHolder1 vh1, int position) {
        User user = (User) items.get(position);
        if (user != null) {
            vh1.getLabel1().setText("Name: " + user.getName());
            vh1.getLabel2().setText("Hometown: " + user.getHomeTown());
        }
    }

    private void configureViewHolder2(ViewHolder2 vh2) {
        vh2.getIvExample().setImageResource(R.drawable.ic_launcher_background);
    }


}

我希望有人能提供帮助。这就是它的样子。如您所见,屏幕上仅显示第一和第二RecylerView。 enter image description here

2 个答案:

答案 0 :(得分:1)

您已在此

中将BronceAdapter设置为SilverRecyclerView。
recyclerViewSilverGroup.setAdapter(bronceGroupRecyclerViewAdapter);

RecyclerView替换为青铜色,应该可以使用。

希望有帮助。

答案 1 :(得分:1)

替换布局的以下部分

ServletContextHandler