首页 > 解决方案 > 我无法在我的书签中看到扩展片段的网格视图

问题描述

我面临一个问题,问题是我可以看到Fragment但不是 GridView我在那里创建的,在LogCat没有错误的情况下什么都没有,但不知何故它不起作用的代码。但是,如果我从 a 中删除GridView并且bookmark.xml只留下 a ,那么TextView我可以表明TextView 我正在尝试将 aGridView放入 a Fragment。这是我的代码。

分段

public class FragmentBookmark extends Fragment {
    View paramView;
    public FragmentBookmark() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        paramView = inflater.inflate(R.layout.bookmark, container, false);
        return paramView;
    }

}

这是主要活动

 ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
 adapter.AddFragment(new FragmentExplore(), "");
 adapter.AddFragment(new FragmentBookmark(), "");
 adapter.AddFragment(new FragmentStore(), "");
 mViewPager.setAdapter(adapter);
 mTabLayout.setupWithViewPager(mViewPager);

这是书签片段的 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="match_parent"
android:gravity="center">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        />

</LinearLayout>

标签: androidxmlandroid-fragments

解决方案


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/m_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp" />
</LinearLayout>

grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="wrap_content"
    app:cardElevation="4dp">

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

        <ImageView
            android:id="@+id/imageview1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="5dp"
            android:contentDescription="@null" />

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:maxLines="1"
            android:singleLine="true"
            tools:text="Category" />
    </LinearLayout>
</android.support.v7.widget.CardView>

适配器:

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;

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

    private Context mContext;
    private List<String> myNameList;

    public MyAdapter(Context context, List<String> categoryInfo) {
        mContext = context;
        myNameList = categoryInfo;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        String myName = myNameList.get(position);
        holder.tvName.setText(myName);
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;

        MyViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }
}

在您的ActivityFragment:

Context mContext = this;
RecyclerView mRecyclerView;
mRecyclerView = findViewById(R.id.m_recyclerView);
mRecyclerView.setLayoutManager(new android.support.v7.widget.GridLayoutManager(mContext, 3));

ArrayList<String> nameArray = new ArrayList<>();
nameArray.add("name 1");
nameArray.add("name 2");
nameArray.add("name 3");
nameArray.add("name 4");

mRecyclerView.setAdapter(new MyAdapter(mContext, nameArray));

推荐阅读