package com.kfzs.cfyl.media.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kfzs.cfyl.media.BaseFragment;
import com.kfzs.cfyl.media.R;
import com.kfzs.cfyl.media.api.IdChooser;
import com.kfzs.cfyl.media.util.G;

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

/**
 * Created by realicing on 2018/12/6.
 * realicing@sina.com
 */
public class FgtDiscoveryTopic extends BaseFragment {

    @Override
    public int getLayoutId() {
        return R.layout.media_common_rv;
    }

    private RecyclerView recyclerView;
    @Override
    public void onViewCreated() {
        recyclerView = findViewById(R.id.recyclerView);
        if(list != null){
            if(recyclerView == null){
                G.showToast(getContext(), "初始化失败");
                return;
            }
            initView();
        }
    }

    public void loadData(ArrayList<String> arrayList) {
        list = arrayList;
        if(recyclerView != null)
            initView();
    }
    private BaseQuickAdapter<String, BaseViewHolder> baseQuickAdapter;
    private void initView(){
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        baseQuickAdapter = getAdapter();
        baseQuickAdapter.bindToRecyclerView(recyclerView);
        baseQuickAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if(getActivity() instanceof IdChooser){
                    ((IdChooser) getActivity()).onSelectId(position);
                }
            }
        });
    }
    private List<String > list;
    protected BaseQuickAdapter<String, BaseViewHolder> getAdapter() {
        return new BaseQuickAdapter<String, BaseViewHolder>(R.layout.media_item_discovery_topic, list) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                helper.setText(R.id.item_discovery_topic_content, item);
            }
        };
    }
}
