我是Recyclerview的新手。我想为每个bottomnavigationview项目显示recyclerview。我该怎么做?

时间:2019-10-26 18:21:37

标签: android android-recyclerview

这是我在stackoverflow中的第一个问题。如果您对任何内容都不了解,我对此感到抱歉:我应该使用片段代替Framelayout吗?

这是我的底部导航

            ```
<FrameLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@id/bottomnavid"
                >

            </FrameLayout>
            <android.support.design.widget.BottomNavigationView
                android:id="@+id/bottomnavid"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                app:menu="@menu/bottom_nav"
                /> 
  ```   

这是我的recyclerview xml的xml:
           ```
            

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

这是我的myadapter类别:

```
            @NonNull
            @Override
            public Myviewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

                View v = LayoutInflater.from(c).inflate(R.layout.cardview,viewGroup,false);
                Myviewholder VH = new Myviewholder(v);
                return VH;
            }
        //replacing the contents of a view 
            @Override
            public void onBindViewHolder(@NonNull Myviewholder myviewholder, int i) {
                myviewholder.nametext.setText(Citynames.get(i));


            }

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

该项目的构建没有任何错误,但未显示recyclerview,我如何在此部分中实现recyclerview,我认为问题在这里。请帮帮我

        public class Feature extends Fragment {
            ArrayList<String> Citynames = new ArrayList<>    (Arrays.asList("dhaka","rongpur","bagura","sylhet","vhola"));
            @Nullable
            @Override
            public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                final View view  = inflater.inflate(R.layout.feature,container,false);
                final FragmentActivity c = getActivity();
                final RecyclerView recyclerView = view.findViewById(R.id.featurerecyclerid);
                LinearLayoutManager layoutManager = new  LinearLayoutManager(c);
                recyclerView.setLayoutManager(layoutManager);
                 Myadapter myadapter = new Myadapter(Citynames,c);
                recyclerView.setAdapter(myadapter);
                return inflater.inflate(R.layout.feature,container,false);
            }
        }


 ```           

1 个答案:

答案 0 :(得分:0)

我不确定您真正想做的是什么,我觉得您对布局有误,在尝试使用回收站视图之前,请先练习一些不同类型的布局,这将有助于您了解设计对象在xmal属性中的工作方式,这样尝试布局

  <FrameLayout
     android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@id/bottomnavid"
                >
     <android.support.v7.widget.RecyclerView
                android:id="@+id/featurerecyclerid"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    <android.support.design.widget.BottomNavigationView
                android:id="@+id/bottomnavid"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                app:menu="@menu/bottom_nav"
                />
            </FrameLayout>

您是否创建了用于初始化R.layout.cardview的任何内部类?另一件事是,您必须在Fragment的RecyclerView方法中使用Adapter初始化onViewCreated()并将其设置为 @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); final FragmentActivity c = getActivity(); final RecyclerView recyclerView = view.findViewById(R.id.featurerecyclerid); LinearLayoutManager layoutManager = new LinearLayoutManager(c); recyclerView.setLayoutManager(layoutManager); Myadapter myadapter = new Myadapter(Citynames,c); recyclerView.setAdapter(myadapter); }

package com.example.finalapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.wajahatkarim3.easyflipview.EasyFlipView;
import java.util.Collections;
import java.util.ArrayList;

public class cardFlipPage extends AppCompatActivity {

    final ImageView[] cards = new ImageView[16];        //Array of Card Objects
    final int[] cardFaces = new int[8];                 //Array of Card Drawables
    ArrayList<Integer> cardValues = new ArrayList();    //Array List of Values corresponding to Drawables (to randomize)
    final EasyFlipView[] card = new EasyFlipView[16];   //Card objects of EasyFlipView classifier


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

    //------------------------ Assign Card Objects and Set Up Click Listeners using setListeners() Function-----------------------------
        cards[0] = findViewById(R.id.card1Front);
        cards[1] = findViewById(R.id.card2Front);
        cards[2] = findViewById(R.id.card3Front);
        cards[3] = findViewById(R.id.card4Front);
        cards[4] = findViewById(R.id.card5Front);
        cards[5] = findViewById(R.id.card6Front);
        cards[6] = findViewById(R.id.card7Front);
        cards[7] = findViewById(R.id.card8Front);
        cards[8] = findViewById(R.id.card9Front);
        cards[9] = findViewById(R.id.card10Front);
        cards[10] = findViewById(R.id.card11Front);
        cards[11] = findViewById(R.id.card12Front);
        cards[12] = findViewById(R.id.card13Front);
        cards[13] = findViewById(R.id.card14Front);
        cards[14] = findViewById(R.id.card15Front);
        cards[15] = findViewById(R.id.card16Front);
        setListeners();

    //-------------------------Assign EasyFlipView Objects based on Card Objects----------------------------------------------------------
        card[0] = findViewById(R.id.card1);
        card[1] = findViewById(R.id.card2);
        card[2] = findViewById(R.id.card3);
        card[3] = findViewById(R.id.card4);
        card[4] = findViewById(R.id.card5);
        card[5] = findViewById(R.id.card6);
        card[6] = findViewById(R.id.card7);
        card[7] = findViewById(R.id.card8);
        card[8] = findViewById(R.id.card9);
        card[9] = findViewById(R.id.card10);
        card[10] = findViewById(R.id.card11);
        card[11] = findViewById(R.id.card12);
        card[12] = findViewById(R.id.card13);
        card[13] = findViewById(R.id.card14);
        card[14] = findViewById(R.id.card15);
        card[15] = findViewById(R.id.card16);

    //-------------------------Assign Card Face Drawables to CardFaces Array for use in for() loop-----------------------------------------
        cardFaces[0] = R.drawable.heartcard;
        cardFaces[1] = R.drawable.starcard;
        cardFaces[2] = R.drawable.xcard;
        cardFaces[3] = R.drawable.arrowcard;
        cardFaces[4] = R.drawable.diamondcard;
        cardFaces[5] = R.drawable.lightningcard;
        cardFaces[6] = R.drawable.mooncard;
        cardFaces[7] = R.drawable.trianglecard;

    //--------------------------Assign integer values to CardFaces in RandPlace() function--------------------------------------------------
        cardValues.add(0);
        cardValues.add(0);
        cardValues.add(1);
        cardValues.add(1);
        cardValues.add(2);
        cardValues.add(2);
        cardValues.add(3);
        cardValues.add(3);
        cardValues.add(4);
        cardValues.add(4);
        cardValues.add(5);
        cardValues.add(5);
        cardValues.add(6);
        cardValues.add(6);
        cardValues.add(7);
        cardValues.add(7);

    //Create board placement through RNG each time the page is created and closed again----------------------------------------------------
        randPlace();
    }


    public void randPlace() {
        for (int x = 0; x <= 15; x++) {                                                             //Iterate 16 times through the for loop, accounting for each of the 16 cards[] objects
            Collections.shuffle(cardValues);                                                        //Shuffle all items in cardValues array to place in pairs
            int y = cardValues.get(0);                                                              //Get the first element in the cardValues array each iteration of the for() loop
            cards[x].setImageResource(cardFaces[y]);                                                //Set the card images to the cardFaces objects of the y value obtained in previous line
            cards[x].setTag(y);                                                                     //Create a tag for each cards[] object for comparison outside the function randPlace()
            cardValues.remove(0);                                                             //Remove the first element in the cardValues array after each iteration to eliminate more than two copies of each card
            cards[x].setPressed(false);
        }
    }

    public void showMatches(int x){
        Integer cardTag = (Integer) cards[x].getTag();                                              //Get tag of specified "x" card in the card[] array for comparison
        for(int y = 0; y <= 15; y++){
            Integer card2Tag = (Integer) cards[y].getTag();                                         //Get tag of card "y" each iteration of the for() loop for comparison
            if(y==x){                                                                               //Add condition to for() loop to skip the specified "x" card so it doesn't compare to itself
                continue;
            }
            else if(cardTag == card2Tag){                                                           //If tag of card "y" is equal to specified card "x", flip it.  Updated each iteration.
                card[y].flipTheView();
            }
        }
    }

    public void setListeners(){
        for(int x = 0; x <= 15; x++){                                                               //Set onClickListeners to each cards[] object during each iteration
            final int finalX = x;
            cards[x].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    card[finalX].flipTheView();
                    showMatches(finalX);
                }
            });
        }
    }
}