Android GridView(自定义)碎片图片,但无法打开页面

时间:2018-09-27 17:36:19

标签: android android-gridview

我想在 Fragement 中创建自定义的GridView, 使用 URLHTTP 获取网址并使用Bitmap

但是我只有一半,

我无法打开MapFragement页面,也找不到我的页面 bug。我不能长时间解决问题,请帮助我。

请帮助或尝试提供一些实现方法,

谢谢!

我的代码:

  

item_gridview_member.xml

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

        <ImageView
            android:id="@+id/imgmember"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </RelativeLayout>
  

fragement_member.xml

       <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout
            android:orientation="vertical"  xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MemberFragment">
          <GridView
              android:id="@+id/gridmember"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:columnWidth="150dp"
              android:stretchMode="columnWidth"
              android:numColumns="3"
              android:verticalSpacing="3dp"
              android:layout_marginBottom="3dp">
          </GridView>
  

CImage.java

    import android.graphics.Bitmap;

    public class CImage {

            private String imagePath;
            private Bitmap bitmap;

        public CImage(String imagePath, Bitmap bitmap) {
            this.imagePath = imagePath;
            this.bitmap = bitmap;
        }

        public String getImagePath() {
                return imagePath;
            }
            public void setImagePath(String imagePath) {
                this.imagePath = imagePath;
            }
            public Bitmap getBitmap() {
                return bitmap;
            }
            public void setBitmap(Bitmap bitmap) {
                this.bitmap = bitmap;
            }

    }
  

GridViewAdapter.java

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;  
    import com.example.yao.pm.R;  
    import java.util.List;   
    public class GridViewAdapter  extends BaseAdapter {

        private LayoutInflater myInflater;

        private List<CImage> ImgList;

        public GridViewAdapter(Context context,List<CImage> ImgList) {
            myInflater = LayoutInflater.from(context);
            this.ImgList = ImgList;
        }


        @Override
        public int getCount() {
            return ImgList.size();
        }

        @Override
        public Object getItem(int position) {
            return ImgList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;


            if(convertView == null)
            {
                convertView = myInflater.inflate(R.layout.item_gridview_member,null);
                holder = new ViewHolder();
                holder.imageView = convertView.findViewById(R.id.image_meme);
                convertView.setTag(holder);
            }

            CImage image = ImgList.get(position);

            holder.imageView.setImageBitmap(image.getBitmap());

            if (image.getBitmap() == null) {
                holder.imageView.setImageResource(R.mipmap.ic_launcher);
            } else {
                holder.imageView.setImageBitmap(image.getBitmap());
            }

            return convertView;
        }

        public class ViewHolder {
            ImageView imageView;
        }

    }
  

MapFregement.java

    public class MemberFragment extends Fragment {


        private GridViewAdapter adapter;

        private ArrayList<CImage> imageList;

        public MemberFragment() {
            // Required empty public constructor
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_member, container, false);

        }
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            inital();
            adapter = new GridViewAdapter(getActivity(),imageList);
            grieview.setAdapter(adapter);

        }

        public void inital(){

            grieview = getActivity().findViewById(R.id.gridmember);

        }
        GridView grieview;
    }

谢谢!

1 个答案:

答案 0 :(得分:1)

研究完代码后,我认为问题出在GridView类中的 MemberFragment

您正在使用充气器将视图放大为片段,并尝试使用活动上下文解析ID。另外,您需要覆盖片段中的onViewCreated()

  

onViewCreated()的用法是什么?

     

在返回 onCreateView(LayoutInflater, ViewGroup, Bundle) 之后但在恢复任何保存状态之前立即调用   进入视图。这使子类有机会初始化   一旦他们知道他们的视图层次结构已经完全   创建。片段的视图层次结构未附加到其片段   家长。


您的 MemberFragment.java

public class MemberFragment extends Fragment {

    private GridView grieview;
    private GridViewAdapter adapter;
    private ArrayList<CImage> imageList;

    public MemberFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_member, container, false);

    }

    //Note: Change is from here, Instead of onActivityCreated(), override this method. Use View object from it.
    @Override
    void onViewCreated (View view, Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        inital(view); // Get the View Object provided from method parameter to find your fragment layout view IDs, It's the same View you return in onCreateView method.
        adapter = new GridViewAdapter(getActivity(),imageList);
        grieview.setAdapter(adapter);
    }

    public void inital(View view){
        grieview = view.findViewById(R.id.gridmember); //Find your view inside fragment like this.
    }

}

希望对您有帮助!