将recyclerview作为gridview?

时间:2016-09-06 14:18:34

标签: android gridview android-recyclerview

我将此Recyclerview作为列表,我必须将其更改为网格视图。怎么办?我尝试了gridlayoutmanger,但它不起作用。我怎么能这样做(并避免错误?感谢您的时间

这是片段:

public class ListViewActivityFragment extends Fragment {
  List<AppShowModule> appShowModule;
  RecyclerView AppRecyclerView;
  List<AppShowModule> GetDataAdapter1;

  private GridLayoutManager lLayout;

  RecyclerView.LayoutManager AppRecyclerViewlayoutManager;
  RecyclerView.Adapter AppRecyclerViewadapter;
  String jsonUrl = "https://itunes.apple.com/jo/rss/topfreeapplications/limit=50/json";
  TextView text;
  Context context;
  RequestQueue requestQueue;
  public ListViewActivityFragment() {
  }
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_list_view, container, false);
  }
  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    appShowModule = new ArrayList<>();
    AppRecyclerView = (RecyclerView) getView().findViewById( R.id.AppRecyclerView );
    AppRecyclerView.setHasFixedSize(true);
    lLayout = new GridLayoutManager(context, 4);
    AppRecyclerView.setLayoutManager(lLayout);
    GetDataAdapter1 = new ArrayList<>();
    JsonAppShowData();
  }

  public void JsonAppShowData() {
    final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( jsonUrl, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONObject("feed").getJSONArray( "entry" );
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject json1 = jsonArray.getJSONObject( i ).getJSONObject("im:name");
                    AppShowModule appShowModule111 = new AppShowModule();
                    String name = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONObject("im:name").getString("label").toString();
                    String image = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONArray("im:image").getJSONObject( 0 ).getString("label").toString();
                    appShowModule111.setAppName((name));
                    appShowModule111.setAppImageUrl( image );
                    GetDataAdapter1.add(appShowModule111);
                }
                AppRecyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1,getContext());
                AppRecyclerView.setAdapter(AppRecyclerViewadapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }}

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e( "LOG", error.toString() );
        }
    } );

    requestQueue = Volley.newRequestQueue( getContext() );
    requestQueue.add(jsonObjectRequest);

    AppRecyclerView.setLayoutManager(new LinearLayoutManager(context));

  }
}

1 个答案:

答案 0 :(得分:0)

在开头本身初始化所有视图和适配器。

onActivityCreated()

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    appShowModule = new ArrayList<>();
    AppRecyclerView = (RecyclerView) getView().findViewById( R.id.AppRecyclerView );
    AppRecyclerView.setHasFixedSize(true);
    lLayout = new GridLayoutManager(context, 4);
    AppRecyclerView.setLayoutManager(lLayout);
    GetDataAdapter1 = new ArrayList<>();
    AppRecyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1,getContext());
    AppRecyclerView.setAdapter(AppRecyclerViewadapter);
    JsonAppShowData();
}

public void JsonAppShowData() {
    final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( jsonUrl, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONObject("feed").getJSONArray( "entry" );
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject json1 = jsonArray.getJSONObject( i ).getJSONObject("im:name");
                    AppShowModule appShowModule111 = new AppShowModule();
                    String name = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONObject("im:name").getString("label").toString();
                    String image = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONArray("im:image").getJSONObject( 0 ).getString("label").toString();
                    appShowModule111.setAppName((name));
                    appShowModule111.setAppImageUrl( image );
                    GetDataAdapter1.add(appShowModule111);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }}

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e( "LOG", error.toString() );
        }
    } );

    requestQueue = Volley.newRequestQueue( getContext() );
    requestQueue.add(jsonObjectRequest);

    // Why are you again setting layout manager. Its already done in onActivityCreated()
    //AppRecyclerView.setLayoutManager(new LinearLayoutManager(context));
    // Just call adapter's notifyDatasetChanged()
    AppRecyclerViewadapter.notifyDatasetChanged();
}