بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]
پرسش و پاسخ

Custom Adapter (آموزش)

مدیریت گفتگو
Hajhosseini 11 سال پیش
آخرین تغییر: 11 سال پیش

با سلام خدمت دوستان عزیز
با توجه به درخواست دوستان، سورس کد یک آداپتر داینامیک که قابلیت گرفتن تایپ های مختلف  رو داره رو درج میکنم.

سورس کد آداپتر:

/**
 * Created by a.hajhosseini on 1/10/2015.
 */
public class ContentAdapterRecyclerTest<T> extends RecyclerView.Adapter<ContentAdapterRecyclerTest.ViewHolder> {
        public ArrayList<T> array;

        public static class ViewHolder extends RecyclerView.ViewHolder {

            // inja View ha ro set mikonid
            TextView txt;
            TextView desc;

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

                // dar View Holder View ha ro cast mikonid
                txt = (TextView) view.findViewById(R.id.itemBookTxtTitle);
                desc = (TextView) view.findViewById(R.id.itemBookTxtDescription);
            }
        }

        public ContentAdapterRecyclerTest(ArrayList<T> items) {
            // gereftane ArrayList
            this.array = items;
        }

        @Override
        public ContentAdapterRecyclerTest.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
            ViewHolder vh = null;
            // CheckKardane viewType ( agar 0 bashe item_main_header ro estefade mikone, agar 1 bashe item_content ro  )
            switch (viewType) {
                case 0:{
                    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_header, parent, false);
                    vh = new ViewHolder(v);
                    break;
                }
                case 1:{
                    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_content, parent, false);
                    vh = new ViewHolder(v);
                    break;
                }
            }
            return vh;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            int type = getItemViewType(position);
            // dar ViewHolder mitoonim be TextView va ... meghdar bedim
            switch(type){
                case 1:{
                    StructContent item = (StructContent) array.get(position);
                    if(item.content_title != null) {
                        holder.txt.setText(item.content_title);
                        SmartFont.setContentTextProperty(holder.txt,0,1.5f,1.5f,SmartFont.getSelectedFontSize());
                        holder.txt.setVisibility(View.VISIBLE);
                    }
                    if(item.content_description != null){
                        holder.desc.setText(item.content_description);
                        SmartFont.setContentTextProperty(holder.desc,0,1.5f,1.5f,SmartFont.getSelectedFontSize());
                        holder.desc.setVisibility(View.VISIBLE);
                    }
                    break;
                }
            }
        }

        @Override
        public int getItemViewType(int position) {
            // dar in ghesmat tedade type hamun ro moshakhas mikonim, man 0 va 1 ro tarif kardam
            if (isPositionHeader(position))
//                it should be 0 if you have header
                return 0;
            return 1;
        }

        @Override
        public int getItemCount() {
            // tedade item haye ArrayList
            return array.size();
        }

        private boolean isPositionHeader(int position) {
            // in method ro man dasti neveshtam, baraye tashkhise type, agar type haye bishtari darid bayad switch bezarid
            if (position < 1)
                return true;
            else
                return false;
        }
}

طریقه استفاده از آداپتر( این کد رو در اکتیویتی بنویسید )

  // items hamun ArrayList mast ke az database gerefte shode
        final ContentAdapterRecyclerTest<StructContent> adapter = new ContentAdapterRecyclerTest<>(items);
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.fragmentMainRecyclerView); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(G.currentActivity, LinearLayoutManager.VERTICAL, false)); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(adapter);

در اولین فرصت آموزش + توضیحات رو اضافه میکنم.

نظرات

هنوز پاسخی ثبت نشده است


پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .