2015-10-06 82 views
0

一般來說,recylceLayout類似於ListView和顯示相同的內容,但是,我不知道還有什麼辦法做到這樣如何在RecycleLayout中設置不同的佈局模式?

1)對於奇數項,請使用odd_item.xml佈局,只運行odd_item行動getView 2)即使使用even_item.xml佈局,只有在getView

這是java,

@Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     rv = (RecyclerView) inflater.inflate(R.layout.fragment_news_list, container, false); 

     app = (MyApp) getActivity().getApplication() ; 

     StringRequest getRequest = new StringRequest(Constant.get_news_list, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String s) { 
         items = new ArrayList<Item>(); 

         try { 
          JSONArray json_array = new JSONArray(s); 
          for (int i = 0; i < json_array.length(); i++) { 
           JSONObject object = json_array.getJSONObject(i); 
           items.add(new Item(object.getString("id"), object.getString("title_tw"), object.getString("image_url"))); 
          } 

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

         setupRecyclerView(rv); 
        } 
       }, 
       new Response.ErrorListener() { 

        @Override 
        public void onErrorResponse(VolleyError volleyError) { 
         Toast.makeText(getActivity(), getResources().getString(R.string.network_err), Toast.LENGTH_LONG).show(); 
        } 
       }); 

     app.mQueue.add(getRequest); 

     return rv; 
    } 


    private void setupRecyclerView(RecyclerView recyclerView) { 
     recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext())); 
     recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(), items)); 
    } 

    public static class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> { 

     private final TypedValue mTypedValue = new TypedValue(); 
     private int mBackground; 
     private List<Item> mValues; 

     public static class ViewHolder extends RecyclerView.ViewHolder { 
      public String id; 

      public final View mView; 
      public final ImageView mImageView; 
      public final TextView mTextView; 

      public ViewHolder(View view) { 
       super(view); 
       mView = view; 
       mImageView = (ImageView) view.findViewById(R.id.avatar); 
       mTextView = (TextView) view.findViewById(android.R.id.text1); 
      } 

      @Override 
      public String toString() { 
       return super.toString() + " '" + mTextView.getText(); 
      } 
     } 

     public SimpleStringRecyclerViewAdapter(Context context, List<Item> items) { 
      context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true); 
      mBackground = mTypedValue.resourceId; 
      mValues = items; 
     } 

     @Override 
     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item_even, parent, false); 
      view.setBackgroundResource(mBackground); 
      return new ViewHolder(view); 
     } 

     @Override 
     public void onBindViewHolder(final ViewHolder holder, int position) { 
      holder.id = mValues.get(position).id; 
      holder.mTextView.setText(mValues.get(position).name); 

      holder.mView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Context context = v.getContext(); 
        Intent intent = new Intent(context, DetailActivity.class); 
        intent.putExtra("id", holder.id); 
        context.startActivity(intent); 
       } 
      }); 

      Glide.with(holder.mImageView.getContext()) 
        .load(Constant.blog_dir + mValues.get(position).img) 
        .fitCenter() 
        .into(holder.mImageView); 
     } 

     @Override 
     public int getItemCount() { 
      return mValues.size(); 
     } 
    } 

如何定製運行even_item行動?非常感謝您的幫助。

回答

1

使用getItemViewType(int position)適配器的方法。通過給定位置處適配器的元素屬性,從該方法返回一個預定義的整數值(例如,對於偶數位置返回一些public static final int VIEW_EVEN = 0等)。該值將作爲viewType參數傳遞給onCreateViewHolder(ViewGroup parent, int viewType)。這使您可以從此方法返回ViewHolder的不同子類,具體取決於viewType值。

相關問題