2016-03-29 26 views
-4

Farklı bir karta tıkladığımda, EvsActivity gibi bir etkinlik çağrılır.Liste görünümünü doldurmanın etkili yolu

Ama hayır. Kartların 100'ü ise o zaman uygun bir çözüm olmayan 100 etkinlik yaratmak zorundayım.

böylece ur yardım cardview koduyla

package com.studyleague.app; 
import android.os.Bundle; 
import android.support.v7.widget.GridLayoutManager; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import java.util.ArrayList; 
import java.util.List; 

import butterknife.Bind; 
import butterknife.ButterKnife; 

public class AvailableContent extends TemplateFragment { 
    @Bind(R.id.available_recycler_view) 
    RecyclerView recyclerView; 

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

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.available_content, container, false); 
     ButterKnife.bind(this, v); 
     recyclerView.setHasFixedSize(true); 
     GridLayoutManager glm = new GridLayoutManager(getActivity(), 2, LinearLayoutManager.VERTICAL, false); 
     recyclerView.setLayoutManager(glm); 

     List<AvailableContentModel> content = new ArrayList<>(); 
     for (int i = 0; i < AvailableContentData.acName.length; i++) { 
      content.add(new AvailableContentModel(AvailableContentData.acIcon[i], AvailableContentData.acName[i])); 
     } 

     final AvailableContentAdapter contentAdapter = new AvailableContentAdapter(getActivity(), content); 
     recyclerView.setAdapter(contentAdapter); 



     return v; 

    } 


} 

// Adaptör //

package com.studyleague.app; 

import android.content.Context; 
import android.content.Intent; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 

import butterknife.Bind; 
import butterknife.ButterKnife; 

public class AvailableContentAdapter extends RecyclerView.Adapter<AvailableContentAdapter.ContentViewHolder> { 

    private final List<AvailableContentModel> dataSet; 
    private Context context; 
    public ArrayList<AvailableContentModel> content; 

    public AvailableContentAdapter(Context context, List<AvailableContentModel> content) { 
     this.context = context; 
     this.dataSet = content; 
    } 

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

    @Override 
    public void onBindViewHolder(final ContentViewHolder hold, final int listPosition) { 
     hold.acImg.setImageResource(dataSet.get(listPosition).getContentImg()); 
     hold.acText.setText(dataSet.get(listPosition).getContentName()); 
    } 

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

    public class ContentViewHolder extends RecyclerView.ViewHolder { 

     @Bind(R.id.available_card_image_view) 
     ImageView acImg; 
     @Bind(R.id.available_card_text_view) 
     TextView acText; 

     public ContentViewHolder(final View view) { 
      super(view); 
      ButterKnife.bind(this, view); 

      view.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        switch (getAdapterPosition()) { 
         case 0: 
          Toast.makeText(v.getContext(), "Mechanics", Toast.LENGTH_SHORT).show(); 
          break; 
         case 1: 
          Toast.makeText(v.getContext(), "B. E. E.", Toast.LENGTH_SHORT).show(); 
          break; 
         case 2: 
          Toast.makeText(v.getContext(), "Maths", Toast.LENGTH_SHORT).show(); 
          break; 
         case 3: 
          Toast.makeText(v.getContext(), "Chemistry", Toast.LENGTH_SHORT).show(); 
          break; 
         case 4: 
          Toast.makeText(v.getContext(), "Physics", Toast.LENGTH_SHORT).show(); 
          break; 
         case 5: 
          context.startActivity(new Intent(context, EvsActivity.class)); 
          break; 
         default: 
          Toast.makeText(v.getContext(), "YOLO", Toast.LENGTH_SHORT).show(); 
          break; 
        } 
       } 
      }); 


     } 
    } 


} 

// Aktivite

// Recyclerview takdir edilecektir plz daha iyi bir çözümle kimse olduğunu karttan birini tıklattığımda çağrıldı: //

package com.studyleague.app; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.ExpandableListView; 
import android.widget.ExpandableListView.OnChildClickListener; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.Collection; 
import java.util.HashMap; 
import java.util.List; 

import butterknife.Bind; 
import butterknife.ButterKnife; 

public class EvsActivity extends AppCompatActivity { 
    @Bind(R.id.exp_list) 
    ExpandableListView expListView; 

    private List<String> chap; 
    private HashMap<String, List<String>> hashMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_evs); 
     ButterKnife.bind(this); 

     // preparing list data 
     prepareListData(); 

     // setting list adapter 
     ExpandableListAdapter listAdapter = new ExpandableListAdapter(this, chap, hashMap); 
     expListView.setAdapter(listAdapter); 

     // expListView on child click listener 
     expListView.setOnChildClickListener(new OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
             int groupPosition, int childPosition, long id) { 
       String groupName = chap.get(groupPosition); 
       String childName = hashMap.get(groupName).get(childPosition); 
       Toast.makeText(getApplication(), groupName + " : " + childName, Toast.LENGTH_SHORT) 
         .show(); 
       Intent i = new Intent(EvsActivity.this, Study.class); 
       startActivity(i); 
       return false; 
      } 
     }); 
    } 

    /* 
    * Preparing the list data 
    */ 
    private void prepareListData() { 

     // Hash map for both header and child 
     hashMap = new HashMap<>(); 

     // Array list for header 
     chap = new ArrayList<>(); 

     // Adding headers to list 
     chap.add("1. Multidisciplinary Nature of Environmental Studies"); 
     chap.add("2. Sustainable Development"); 
     chap.add("3. Environmental Pollution"); 
     chap.add("4. Environmental Legislation"); 
     chap.add("5. Renewable Sources of Energy"); 
     chap.add("6. Environment and Technology"); 
     chap.add("7. Another Chapter"); 
     chap.add("8. Almost Another Chapter"); 

     // Array list for child items 
     List<String> child1 = new ArrayList<>(fillChildData(1, 5)); 
     List<String> child2 = new ArrayList<>(fillChildData(2, 3)); 
     List<String> child3 = new ArrayList<>(fillChildData(3, 11)); 
     List<String> child4 = new ArrayList<>(fillChildData(4, 6)); 
     List<String> child5 = new ArrayList<>(fillChildData(5, 4)); 
     List<String> child6 = new ArrayList<>(fillChildData(6, 2)); 
     List<String> child7 = new ArrayList<>(fillChildData(7, 2)); 
     List<String> child8 = new ArrayList<>(fillChildData(8, 2)); 

     // Adding header and children to hash map 
     hashMap.put(chap.get(0), child1); 
     hashMap.put(chap.get(1), child2); 
     hashMap.put(chap.get(2), child3); 
     hashMap.put(chap.get(3), child4); 
     hashMap.put(chap.get(4), child5); 
     hashMap.put(chap.get(5), child6); 
     hashMap.put(chap.get(6), child7); 
     hashMap.put(chap.get(7), child8); 
    } 

    private Collection<String> fillChildData(int index, int n) { 
     List<String> list = new ArrayList<>(); 
     for (int i = 0; i < n; i++) { 
      list.add("Group " + index + " - Child : " + (i + 1)); 
     } 
     return list; 
    } 
} 
Verilerin

// Kod örtmek

package com.studyleague.app; 

public class AvailableContentData { 

    static final Integer[] acIcon = { 
      R.mipmap.mec, 
      R.mipmap.elec, 
      R.mipmap.mat, 
      R.mipmap.chem, 
      R.mipmap.ic, 
      R.mipmap.env 
    }; 
    static final String[] acName = { 
      "Mechanics", 
      "B. E. E.", 
      "Maths", 
      "Chemistry", 
      "Physics", 
      "E. V. S." 
    }; 
} 
+1

R.layout.whale_items Bu soru oldukça belirsizdir. Kodunuzu göstermek, beklediğiniz sonucu ve bunun sizin için nasıl gerçekleşmediğini de dahil olmak üzere, yaşadığınız sorunu açık bir şekilde açıklamak daha iyi olacaktır. – NoChinDeluxe

+0

Hedefiniz nedir? yanlış bir yaklaşım izliyor olabilirsiniz, lütfen ne istediğinizi açıklığa kavuşturun, böylece – Sam

+0

@NoChinDeluxe ve sam'a yardım edebiliriz. – Ace

cevap

0

yanlış hesap altında bu kadar Yazılan // gerekli (yorum yapamam, üzgünüm), sanırım sopa vermedi. Bir ListView ile bir Etkinlik oluşturun ve CardView amacından kullanmak istediğiniz bağdaştırıcıyı iletin.

Kelebek Cardview -> KELEBEK -> ButterflyAdapter

Baseball Bat Cardview -> BEYZBOL -> BaseballBatAdatper senin ürün alakalı değilse

. Onlar ilişkin ise aynı adaptör kullanabilirsiniz ama gönderilen dayalı farklı bir düzende göndermek

Kelebek -.>

Balina R.layout.butterfly_items ->

İlgili konular