我无法在扩展了Fragment的书签中看到网格视图

时间:2018-11-22 12:11:47

标签: android xml android-fragments

我遇到一个问题,问题是我可以看到Fragment,但看不到 我在其中创建的GridViewLogCat中没有错误,不过没有错误,但是代码不起作用。 但是,如果我从GridView中删除了bookmark.xml,只留下了TextView,则我无法显示TextView 我正在尝试将GridView变成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>

3 个答案:

答案 0 :(得分:0)

尝试使用framelayout

<FrameLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
    android:id = "@+id/gvList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="spacingWidth"
    android:numColumns="2"/>
</FrameLayout>

答案 1 :(得分:0)

您需要将adapter设置为GridView,因为只有GridView永远不会显示任何内容,因为它是一个简单的ViewGroup。此外,也不要使用GridView来尝试使用RecyclerView在网格模式下。以下是您可以遵循的简单教程- RecyclerView as GridView with GridLayoutManager

答案 2 :(得分:0)

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));