2017-08-03 81 views
0

我想創建水平recyclerview內像垂直recyclerview像谷歌播放存儲和獲取數據與齊射和顯示,我看到這個示例http://android-pratap.blogspot.co.uk/2015/12/horizontal-recyclerview-in-vertical.html 但不使用volley顯示數據在recyclerview ,如何使用這個API來顯示數據在回收站水平回收站內垂直回收站查看排球? API:http://monaasoft.com/indianfm/api/test.json 請幫助我在垂直recyclerview創建水平recyclerview像谷歌播放商店與凌空

數據

public class Data { 


private String title; 
private List<Section> section; 


public Data(){ 

} 

public Data(String title,List<Section> sections){ 
    this.title=title; 
    this.section=section; 
} 


public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public List<Section> getSection() { 
    return section; 
} 

public void setSection(List<Section> section) { 
    this.section = section; 
} 


} 

public class Section { 
private String name; 
private String image; 


public Section(){ 

} 


public Section(String name,String image){ 
    this.name=name; 
    this.image=image; 
} 

public String getImage() { 
    return image; 
} 

public void setImage(String image) { 
    this.image = image; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 


} 

RecyclerViewDataAdapter

public class RecyclerViewDataAdapter extends 
RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder> { 

private List<Data> dataList; 
private Context mContext; 

public RecyclerViewDataAdapter(Context context, List<Data> dataList) { 
    this.dataList = dataList; 
    this.mContext = context; 
} 



@Override 
public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View v = 
LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, 
null); 
    ItemRowHolder mh = new ItemRowHolder(v); 
    return mh; 
} 

@Override 
public void onBindViewHolder(ItemRowHolder itemRowHolder, int i) { 

    final String sectionName = dataList.get(i).getTitle(); 

    List singleSectionItems = dataList.get(i).getSection(); 

    itemRowHolder.itemTitle.setText(sectionName); 

    SectionListDataAdapter itemListDataAdapter = new 
    SectionListDataAdapter(mContext, singleSectionItems); 

    itemRowHolder.recycler_view_list.setHasFixedSize(true); 
    itemRowHolder.recycler_view_list.setLayoutManager(new 
    LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false)); 
    itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter); 


    itemRowHolder.recycler_view_list.setNestedScrollingEnabled(false); 


    /* itemRowHolder.recycler_view_list.setOnTouchListener(new 
    View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      int action = event.getAction(); 
      switch (action) { 
       case MotionEvent.ACTION_DOWN: 
        // Disallow ScrollView to intercept touch events. 
        v.getParent().requestDisallowInterceptTouchEvent(true); 
        break; 
       case MotionEvent.ACTION_UP: 
        //Allow ScrollView to intercept touch events once again. 
        v.getParent().requestDisallowInterceptTouchEvent(false); 
        break; 
      } 
      // Handle RecyclerView touch events. 
      v.onTouchEvent(event); 
      return true; 
     } 
    });*/ 

    itemRowHolder.btnMore.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      Toast.makeText(v.getContext(), "click event on more, 
    "+sectionName , Toast.LENGTH_SHORT).show(); 



     } 
    }); 







    /* Glide.with(mContext) 
      .load(feedItem.getImageURL()) 
      .diskCacheStrategy(DiskCacheStrategy.ALL) 
      .centerCrop() 
      .error(R.drawable.bg) 
      .into(feedListRowHolder.thumbView);*/ 
} 

@Override 
public int getItemCount() { 
    return (null != dataList ? dataList.size() : 0); 
} 

public class ItemRowHolder extends RecyclerView.ViewHolder { 

    protected TextView itemTitle; 

    protected RecyclerView recycler_view_list; 

    protected Button btnMore; 



    public ItemRowHolder(View view) { 
     super(view); 

     this.itemTitle = (TextView) view.findViewById(R.id.itemTitle); 
     this.recycler_view_list = (RecyclerView) view.findViewById(R.id.recycler_view_list); 
     this.btnMore= (Button) view.findViewById(R.id.btnMore); 


    } 

} 

} 

SectionListDataAdapter

public class SectionListDataAdapter extends 
RecyclerView.Adapter<SectionListDataAdapter.SingleItemRowHolder> { 

private List<Section> itemsList; 
private Context mContext; 

public SectionListDataAdapter(Context context, List<Section> itemsList) { 
    this.itemsList = itemsList; 
    this.mContext = context; 
} 

@Override 
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View v = 


LayoutInflater.from(viewGroup.getContext()) 
.inflate(R.layout.list_single_card, 
    null); 
    SingleItemRowHolder mh = new SingleItemRowHolder(v); 
    return mh; 
} 

@Override 
public void onBindViewHolder(SingleItemRowHolder holder, int i) { 

    Section singleItem = itemsList.get(i); 

    holder.tvTitle.setText(singleItem.getName()); 


    Glide.with(mContext) 
      .load(singleItem.getImage()) 
      .diskCacheStrategy(DiskCacheStrategy.ALL) 
      .centerCrop() 
      .into(holder.itemImage); 
} 

@Override 
public int getItemCount() { 
    return (null != itemsList ? itemsList.size() : 0); 
} 

public class SingleItemRowHolder extends RecyclerView.ViewHolder { 

    protected TextView tvTitle; 

    protected ImageView itemImage; 


    public SingleItemRowHolder(View view) { 
     super(view); 

     this.tvTitle = (TextView) view.findViewById(R.id.tvTitle); 
     this.itemImage = (ImageView) view.findViewById(R.id.itemImage); 


     view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       Toast.makeText(v.getContext(), tvTitle.getText(), 
     Toast.LENGTH_SHORT).show(); 

      } 
     }); 


    } 

} 

    } 

MainAcivity

public class MainActivity extends AppCompatActivity { 
private Toolbar toolbar; 
private RecyclerView my_recycler_view; 
private RecyclerView.LayoutManager layoutManager; 
private RecyclerView.Adapter adapter; 
List<Data> allSampleData; 
private String TEST_URL="http://monaasoft.com/indianfm/api/test.json"; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    allSampleData=new ArrayList<Data>(); 
    my_recycler_view = (RecyclerView) findViewById(R.id.my_recycler_view); 

    my_recycler_view.setHasFixedSize(true); 

    RecyclerViewDataAdapter adapter = new 
    RecyclerViewDataAdapter(MainActivity.this, allSampleData); 

    my_recycler_view.setLayoutManager(new 
    LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, 
    false)); 

    my_recycler_view.setAdapter(adapter); 



JsonArrayRequest newreq=new JsonArrayRequest(TEST_URL, new 
Response.Listener<JSONArray>() { 
@Override 
    public void onResponse(JSONArray response) { 


    Log.d("main",""+response); 





     for (int i=0 ; i<=response.length();i++){ 
     try { 
      JSONObject sectionObj= (JSONObject) response.get(i); 
      String title=sectionObj.getString("title"); 
      List<Section> sections=new ArrayList<Section>(); 


      JSONArray sectionArray=sectionObj.getJSONArray("section"); 
      for(int j=0;j<sectionArray.length();j++){ 
       JSONObject obj=(JSONObject) sectionArray.get(j); 
       Section section = new Section(); 

       section.setName(obj.getString("name")); 
       section.setImage(obj.getString("image")); 
       sections.add(section); 

       Data data= new Data(); 

       data.setTitle(title); 
       data.setSection(sections); 


       allSampleData.add(data); 

      } 

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


    } 
     }, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 

    } 
    }); 


    MySingleton.getInstance(this).addToRequestQueue(newreq); 

       } 



     } 

MySingleton

public class MySingleton { 
    private static MySingleton mInstance; 
    private RequestQueue mRequestQueue; 
    private ImageLoader mImageLoader; 
    private static Context mCtx; 

    private MySingleton(Context context) { 
     mCtx = context; 
     mRequestQueue = getRequestQueue(); 

     mImageLoader = new ImageLoader(mRequestQueue, 
       new ImageLoader.ImageCache() { 
        private final LruCache<String, Bitmap> 
          cache = new LruCache<String, Bitmap>(20); 

        @Override 
        public Bitmap getBitmap(String url) { 
         return cache.get(url); 
        } 

        @Override 
        public void putBitmap(String url, Bitmap bitmap) { 
         cache.put(url, bitmap); 
        } 
       }); 
    } 

    public static synchronized MySingleton getInstance(Context context) { 
     if (mInstance == null) { 
      mInstance = new MySingleton(context); 
     } 
     return mInstance; 
    } 

    public RequestQueue getRequestQueue() { 
     if (mRequestQueue == null) { 
      // getApplicationContext() is key, it keeps you from leaking the 
      // Activity or BroadcastReceiver if someone passes one in. 
      mRequestQueue = 
      Volley.newRequestQueue(mCtx.getApplicationContext()); 
     } 
     return mRequestQueue; 
    } 

    public <T> void addToRequestQueue(Request<T> req) { 
     getRequestQueue().add(req); 
    } 

    public ImageLoader getImageLoader() { 
     return mImageLoader; 
    } 
} 

回答

1

也許你應該閱讀有關如何使用排一些教程。

http://www.infuy.com/blog/networking-for-android-made-easy-the-volley-library/

http://stackoverflow.com/questions/16902716/comparison-of-android-networking-libraries-okhttp-retrofit-volley

http://instructure.github.io/blog/2013/12/09/volley-vs-retrofit/

https://gist.github.com/ficusk/5474673

http://www.hackpundit.com/android-turorial-json-parse-volley/

https://www.youtube.com/watch?v=yhv8l9F44qo&feature=youtu.be

http://androidbackstage.blogspot.in/2014/05/android-developers-backstage-episode-8.html

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

http://www.androidhive.info/2014/09/android-json-parsing-using-volley/

http://code.tutsplus.com/tutorials/creating-a-weather-application-for-mars-using-volley--cms-23812

http://karn-neelmani.blogspot.in/2014/12/post-json-object-to-server-using-volley.html

http://mobilesiri.com/android-custom-listview-tutorial-using-volley-networkimageview-android-studio/

http://www.truiton.com/2015/03/android-volley-imageloader-networkimageview-example

http://www.truiton.com/2015/02/android-volley-example/

https://www.youtube.com/watch?v=hJRjCurwXVw&index=30&list=PLonJJ3BVjZW6CtAMbJz1XD8ELUs1KXaTD

http://afzaln.com/volley/

https://plus.google.com/111851281564260689455/posts/ZxWapXmrqfF

http://blog.codeint.com/sending-custom-headers-with-volley-android-networking-library/

http://cypressnorth.com/mobile-application-development/setting-android-google-volley-imageloader-networkimageview/

https://github.com/mcxiaoke/android-volley

https://android.googlesource.com/platform/frameworks/volley/

http://narasimha-android.blogspot.in/2015/06/comparison-between-android-networking.html

http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/

https://medium.com/@ali.muzaffar/is-retrofit-faster-than-volley-the-answer-may-surprise-you-4379bc589d7c#.g7v2top6w

http://www.itsalif.info/content/android-volley-tutorial-http-get-post-put

https://www.sitepoint.com/volley-a-networking-library-for-android/

https://www.bignerdranch.com/blog/solving-the-android-image-loading-problem-volley-vs-picasso/

+0

我用了很多這樣的排球,但是隻能在vertical recyclerview裏面的水平recyclerview中使用排氣! –

+0

它怎麼可能?凌空只是從你發送的網絡請求接收數據,它與回收站之間沒有任何關係 –

相關問題