web-dev-qa-db-ja.com

リサイクルビューが表示されない

私は、ナビゲーションドロワーのリサイクラービューを使用するアプリを作成しており、リサイクラービューのコンテンツが表示されていない方法を説明しています。私はスクロールの影を見ることができるので、ビューは間違いなく彼らのものです。アプリが実行されてもクラッシュしないので、間違ったことをしたかどうかわかりません。

ナビゲーションドロワーフラグメント:

public class NavigationDrawerFragment extends Android.support.v4.app.Fragment {

    public static final String PREF_FILE_NAME = "testPref";
    public static final String KEY_USER_LEARNED_DRAWER = "user_learned_drawer";
    private RecyclerView mRecyclerView;
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private View containerView;
    private Boolean mUserLearnedDrawer;
    private Boolean mFromSavedInstanceState;
    private DrawerAdapter adapter;

    public NavigationDrawerFragment() {
        mFromSavedInstanceState = false;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, "false"));
        if (savedInstanceState != null){
            mFromSavedInstanceState = true;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
        mRecyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
        adapter = new DrawerAdapter(getActivity(), getData());
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return layout;
    }

    public static List<DrawerRow> getData(){
        List<DrawerRow> data = new ArrayList<>();
        int icons[] = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
        String titles[] = {"Link 1","Link 2","Link 3","Link 4"};
        for (int i = 0 ; i < titles.length && i < icons.length; i++){
            DrawerRow current = new DrawerRow();
            current.iconId = icons[i];
            current.title = titles[i];
            data.add(current);
        }

        return data;
    }

    public void setUp(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
        containerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClosed){

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if(!mUserLearnedDrawer){
                    mUserLearnedDrawer = true;
                    saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWER, mUserLearnedDrawer+"");
                }
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActivity().invalidateOptionsMenu();
            }
        };

        if(!mUserLearnedDrawer && !mFromSavedInstanceState){
            mDrawerLayout.openDrawer(containerView);
        }

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });
    }

    public void saveToPreferences(Context context, String prefName, String prefValue){
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor mEditor = sharedPreferences.edit();
        mEditor.putString(prefName, prefValue);
        mEditor.apply();
    }

    public static String readFromPreferences(Context context, String prefName, String defaultValue){
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(prefName,defaultValue);
    }
}

アダプタ:

public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.MyViewHolder>{

    private LayoutInflater inflator;
    List<DrawerRow> data = Collections.EMPTY_LIST;

    public DrawerAdapter(FragmentActivity activity, List<DrawerRow> data) {
        inflator = LayoutInflater.from(activity);
        this.data = data;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = inflator.inflate(R.layout.nav_drawer_row , viewGroup,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder viewHolder, int i) {
        DrawerRow current = data.get(i);
        viewHolder.title.setText(current.title);
        viewHolder.icon.setImageResource(current.iconId);
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView icon;
        TextView title;

        public MyViewHolder(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.list_icon);
            title = (TextView) itemView.findViewById(R.id.list_text);
        }
    }
}

引き出し列:

public class DrawerRow {
    int iconId;
    String title;
}

レイアウト:

<Android.support.v4.widget.DrawerLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:id="@+id/drawerLayout">

    <!--Main Content -->
    <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:orientation="vertical">

        <include
            Android:id="@+id/app_bar"
            layout="@layout/app_bar" />

        <TextView
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:text="New Text" />

    </LinearLayout>

    <!--Nav Drawer -->
    <fragment
        Android:layout_width="@dimen/nav_drawer_width"
        Android:layout_height="match_parent"
        Android:layout_gravity="start"
        Android:name="uk.co.clickcomputing.hhscweatherstation.NavigationDrawerFragment"
        Android:id="@+id/fragment_navigation_drawer"/>

</Android.support.v4.widget.DrawerLayout>

引き出し列:

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

    <ImageView
        Android:padding="@dimen/list_padding"
        Android:id="@+id/list_icon"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_gravity="center_vertical"
        Android:src="@drawable/ic_launcher"/>

    <TextView
        Android:padding="@dimen/list_padding"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_gravity="center_vertical"
        Android:text="New Text"
        Android:id="@+id/list_text" />
</LinearLayout>

ナビゲーションドロワーフラグメントレイアウト:

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:background="#eee"
    Android:orientation="vertical">

        <ImageView
            Android:layout_width="match_parent"
            Android:layout_height="140dp"
            Android:layout_marginBottom="8dp"
            Android:contentDescription="@string/nav_drawer_img_desciption"
            Android:scaleType="centerCrop"
            Android:src="@drawable/nav_drawer" />

        <Android.support.v7.widget.RecyclerView
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:id="@+id/drawerList">

        </Android.support.v7.widget.RecyclerView>
    </LinearLayout>
22
cheese12345

問題は、表示する行がないことを示すアイテムカウントを0として返すことです。以下に示すように、リストのサイズを返す必要があります。

    @Override
    public int getItemCount() {
        return data.size();
    }
62
Psypher

次の行を置き換えます

 mRecyclerView.setAdapter(adapter);
 mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));    
mRecyclerView.setAdapter(adapter);
12
Mangal

正しいアイテム番号を返す必要があることに同意しますが、リサイクラビューの高さをwrap_contentに設定することにも問題があると思います。 this リンクはとても役に立ちました。それが役に立てば幸い。

5
Zhivko Dunovski