android - 我无法在我的书签中看到扩展片段的网格视图
问题描述
我面临一个问题,问题是我可以看到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>
解决方案
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);
}
}
}
在您的Activity
或Fragment:
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));
推荐阅读
- visual-studio - ODBC 驱动程序不匹配 VS
- wordpress - WooCommerce 为客人报告
- javascript - 在传统的 HTML/CSS/JS 环境中使用 ES6 导入构建 React?
- discord - 如何检查消息是否通过特定渠道发送。频道 ID discord.py?
- python - 有没有办法计算特定excel列中每一行的字数?
- asp.net - 具有 SSMS 和复制功能的 ASP.NET + MS SQL Server 托管服务提供商,可与本地 SQL Server 数据库同步
- c# - .net services.AddHttpClient 自动访问令牌处理
- swift - 如何在 Xcode/Swift com.apple.developer.healthkit.background-delivery 中添加此权利?
- c++ - 使用 OpenMP 查找大数的所有除数的正确方法
- parsing - 如何使用 rust 和 pomelo 以 backus naur 形式表示 if-else 语句?