如何在recyclerview android中手动创建小部件(动态)

时间:2016-03-29 22:11:00

标签: android android-studio android-recyclerview android-viewholder

我正在尝试创建动态RecyclerView。这里已经有很多很好的例子,但无法得出我的疑问。 我的问题是无法理解如何在不与wedgets相关的情况下手动创建xml。我也需要创造。

我需要创建与RadioButtons无关的CheckBoxEditTextTextViewxml和必要的内容。

按照下面的课程:

public class AdapterPergunta extends RecyclerView.Adapter<AdapterPergunta.MyViewHolder> {
private ArrayAdapter<PerguntaPOJO> adpPergunta;
//private ArrayAdapter<RespostaPOJO> adpResposta;
private List<RespostaPOJO> listResposta;
private RespostaDAO respostaDAO;
private LayoutInflater mLayoutInflater;
private int mTipoPergunta;
private RecyclerViewOnClickListenerAdapterPergunta mRecyclerViewOnClickListenerAdapterPergunta;
private Context mcontext;
private RadioGroup myRadiogroup;
private RadioButton myRadiobutton;
private PerguntaPOJO perguntaPOJO;
private int contPergunta;
private boolean alimentou;

private static final int CREATE_RADIOBUTTON = 0;
private static final int CREATE_CHECKBOX = 1;
private static final int CREATE_EDITTEXT = 2;
private static final int CREATE_TEXTVIEW = 0;


public AdapterPergunta(Context context, ArrayAdapter<PerguntaPOJO> adp){
    adpPergunta = adp;
    mcontext = context;
    mLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    respostaDAO = new RespostaDAO(mcontext);
    perguntaPOJO = new PerguntaPOJO();
    contPergunta=0;
    alimentou = false;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //Chamado quando tem necessidade de criar uma nova View
    View view = mLayoutInflater.inflate(R.layout.item_adapter_pergunta_recyclerview, parent, false);
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    listResposta = respostaDAO.listaRespostaAdapter(mcontext, adpPergunta.getItem(position).getIdpergunta());
    alimentou = true;
    contPergunta = contPergunta + 1;

    try {
        switch (holder.getItemViewType()) {
            case CREATE_TEXTVIEW:
                GrupoPergunta textView = (GrupoPergunta) holder;
                textView.mTextView.setText(adpPergunta.getItem(position).getDescricao());
                textView.mTextView.setId(adpPergunta.getItem(position).getIdpergunta());
                break;
        }
    } catch (Exception e) {
        Log.d("Erro ao criar GrupoPergunta", e.toString());
    }
    Log.d("Position: ", String.valueOf(position));
    Log.d("Pergunta: ",String.valueOf(adpPergunta.getItem(position).getIdpergunta()+"-"+
            adpPergunta.getItem(position).getDescricao()));

    //adpResposta.clear();
    try {
        YoYo.with(Techniques.BounceInUp)
                .duration(700)
                .playOn(holder.itemView);
    } catch (Exception e) {

    }
}

@Override
public int getItemCount() {
    //tamanho da lista
    int count = adpPergunta.getCount();
    return count;
}

@Override
public int getItemViewType(int position) {
    int posicao = 0;
    switch (position) {
        case 0:
            return CREATE_TEXTVIEW;
    }
    return posicao;
}

public class GrupoPergunta extends MyViewHolder{
    TextView mTextView;
    RadioGroup mRadioGroup;
    RadioButton mRadioBuitton;
    CheckBox mCheckBox;
    EditText mEditText;
    public GrupoPergunta(View itemView) {
        super(itemView);
        itemView.setOnClickListener((View.OnClickListener) itemView);
        mTextView = (TextView)itemView.findViewById(R.id.tvItemPerguntaViewHolver);

    }
}


public void setRecyclerViewOnClickListenerAdapterPergunta(RecyclerViewOnClickListenerAdapterPergunta r) {
    mRecyclerViewOnClickListenerAdapterPergunta = r;
}

public void addAdpterItem(PerguntaPOJO perguntaPOJO, int position) {
    adpPergunta.add(perguntaPOJO);
    notifyItemInserted(position);
}

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView tv_pergunta_adapter;
    public RadioGroup rgItemAdapter;
    public RadioButton rbItemAdapter;
    public LinearLayout lnItemAdapterPergunta;
    public MyViewHolder(View itemView) {
        super(itemView);

        tv_pergunta_adapter = (TextView)itemView.findViewById(R.id.tv_pegunta_adapter);
        lnItemAdapterPergunta = (LinearLayout)itemView.findViewById(R.id.lnItemAdapterPergunta);

        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mRecyclerViewOnClickListenerAdapterPergunta != null) {
            mRecyclerViewOnClickListenerAdapterPergunta.onClickListener(v, getLayoutPosition());
        }
    }
}

}

我希望我已经很好地解释了我的问题。

0 个答案:

没有答案