Я не могу видеть вид сетки в моей закладке, которая расширяет фрагмент - PullRequest
0 голосов
/ 22 ноября 2018

Я столкнулся с проблемой, проблема в том, что я вижу Fragment, но не GridView, который я там создал, в LogCat нет ошибок, но почему-то код не работает.Но если я удаляю 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 голосов
/ 22 ноября 2018

Вам нужно установить adapter на GridView, так как только GridView никогда ничего не покажет, так как это простая ViewGroup. Кроме того, вместо использования GridView попробуйте использовать RecyclerView в режиме сетки.Ниже приведен простой учебник, которому вы можете следовать - RecyclerView как GridView с GridLayoutManager

0 голосов
/ 22 ноября 2018

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));
0 голосов
/ 22 ноября 2018

Попробуйте вот так, используя 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...