如何在选项卡片段内调用gridView适配器

Posted

技术标签:

【中文标题】如何在选项卡片段内调用gridView适配器【英文标题】:How to call gridView adapter inside tab fragment 【发布时间】:2018-02-24 19:19:35 【问题描述】:

错误:(117, 63) 错误:不兼容的类型:TabThreeFragment 无法转换为上下文

错误显示在“new GridViewAdapter(this,images,names);

公共类 TabThreeFragment 扩展片段

//url for grid images
public static final String DATA_URL = "https://gist.githubusercontent.com/theBoyMo/40b97e688f90a68bfc02/raw/c8463217c22e597c316edb059db410fa38ec26dc/gallery.json";

//Tag values to read from json
public static final String TAG_IMAGE_URL = "image";
public static final String TAG_NAME = "caption";

//GridView Object
private GridView gridView;

//ArrayList for Storing image urls and titles
private ArrayList<String> images;
private ArrayList<String> names;


@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
    View rootView = inflater.inflate(R.layout.tab_three_fragment, container, false);

    // Inflate the layout for this fragment
    gridView = (GridView) rootView.findViewById(R.id.gridView);

    images = new ArrayList<>();
    names = new ArrayList<>();

    getData();
   return rootView;

// return inflater.inflate(R.layout.tab_three_fragment, container, false);

private void getData()
    //Showing a progress dialog while our app fetches the data from url
   // final ProgressDialog loading = ProgressDialog.show(this, "Please wait...","Fetching data...",false,false);

    //Creating a json array request to get the json from our api
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL,
            new Response.Listener<JSONArray>() 
                @Override
                public void onResponse(JSONArray response) 
                    //Dismissing the progressdialog on response
                  //  loading.dismiss();

                    //Displaying our grid
                    showGrid(response);
                
            ,
            new Response.ErrorListener() 
                @Override
                public void onErrorResponse(VolleyError error) 

                
            
    );

    //Creating a request queue

// RequestQueue requestQueue = Volley.newRequestQueue(this); RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext()); //将我们的请求加入队列 requestQueue.add(jsonArrayRequest);

private void showGrid(JSONArray jsonArray)
    //Looping through all the elements of json array
    for(int i = 0; i<jsonArray.length(); i++)
        //Creating a json object of the current index
        JSONObject obj = null;
        try 
            //getting json object from current index
            obj = jsonArray.getJSONObject(i);

            //getting image url and title from json object
            images.add(obj.getString(TAG_IMAGE_URL));
            names.add(obj.getString(TAG_NAME));
         catch (JSONException e) 
            e.printStackTrace();
        
    
    //Creating GridViewAdapter Object
    GridViewAdapter gridViewAdapter = new GridViewAdapter(this,images,names);

    //Adding adapter to gridview
    gridView.setAdapter(gridViewAdapter);

我的适配器是

公共类 GridViewAdapter 扩展 BaseAdapter

//Imageloader to load images
private ImageLoader imageLoader;

//Context
private Context context;

//Array List that would contain the urls and the titles for the images
private ArrayList<String> images;
private ArrayList<String> names;

public GridViewAdapter (Context context, ArrayList<String> images, ArrayList<String> names)
    //Getting all the values
    this.context = context;
    this.images = images;
    this.names = names;


@Override
public int getCount() 
    return images.size();


@Override
public Object getItem(int position) 
    return images.get(position);


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


@Override
public View getView(int position, View convertView, ViewGroup parent) 
    //Creating a linear layout
    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    //NetworkImageView
    NetworkImageView networkImageView = new NetworkImageView(context);

    //Initializing ImageLoader
    imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
   // imageLoader.get(images.get(position), ImageLoader.getImageListener(networkImageView, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

    //Setting the image url to load
    networkImageView.setImageUrl(images.get(position),imageLoader);

    //Creating a textview to show the title
    TextView textView = new TextView(context);
    textView.setText(names.get(position));

    //Scaling the imageview
    networkImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    //networkImageView.setLayoutParams(new GridView.LayoutParams(200,200));

    //Adding views to the layout
    //linearLayout.addView(textView);
    linearLayout.addView(networkImageView);

    //Returnint the layout
    return linearLayout;

【问题讨论】:

将您的代码粘贴到 GridViewAdapter 类中。将 new GridViewAdapter(this,images, names) 替换为 new GridViewAdapter(getActivity, images, names) 【参考方案1】:

在 TabThreeFragment 中,替换

GridViewAdapter gridViewAdapter = new GridViewAdapter(this,images,names);

GridViewAdapter gridViewAdapter = new GridViewAdapter(getContext(),images,names);

片段不从上下文扩展。

【讨论】:

以上是关于如何在选项卡片段内调用gridView适配器的主要内容,如果未能解决你的问题,请参考以下文章

如何在每个导航选项卡的片段内创建 2 个片段

从适配器中的片段调用元素

Android:如何在选项卡内从一个片段导航到另一个片段? [关闭]

检测片段内的 ViewPager 选项卡更改

检测片段内的 ViewPager 选项卡更改

如何缓存片段视图