如何在同一活动中多次单击按钮

时间:2016-12-25 10:41:41

标签: android android-recyclerview

我在TextView中有ButtonRecyclerView我将Button上的文字作为数字当我点击按钮时,我希望该数字在我尝试递归时增加1 {1}}该应用程序无法正常运行 我有两个活动,每个活动都调用相同的适配器 适配器代码

setonclicklistener

第一项活动

    public class AzkaarAdapter extends RecyclerView.Adapter<AzkaarAdapter.AzkaarViewHolder>  {
    ArrayList<AzkarForm> azkarForms=new ArrayList<AzkarForm>();
    Context ctx;

    private static ItemClickCallback itemClickCallback;

    public interface ItemClickCallback {
        void onItemClick(int p);
        void onSecondaryIconClick(int p);
    }

    public void setItemClickCallback(final ItemClickCallback itemClickCallback) {
        this.itemClickCallback = itemClickCallback;
    }
    public  AzkaarAdapter (ArrayList<AzkarForm> azkarForms,Context ctx){
        this.azkarForms=azkarForms;
        this.ctx=ctx;
    }
    @Override
    public AzkaarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        AzkaarViewHolder azkaarViewHolder;
        if(ctx instanceof MainActivity){
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout,parent,false);
         azkaarViewHolder=new AzkaarViewHolder(view,ctx,azkarForms);
        }
        else {
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_details,parent,false);
             azkaarViewHolder=new AzkaarViewHolder(view,ctx,azkarForms);
        }
        return azkaarViewHolder;
    }


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

        AzkarForm azkarForm=azkarForms.get(position);
        if(ctx instanceof MainActivity) {
            holder.sample.setText(azkarForm.getComplete_morning());
            holder.number.setText(String.valueOf(azkarForm.getNumber()));
        }else {
            holder.sample.setText(azkarForm.getComplete_morning());
            holder.tally_count.setText(getTextNumber(azkarForm.getNumber()));

        }

    }

    public String getTextNumber(int number){
        String numberText="";
        switch (number){
            case 1:
                numberText="مرة واحدة";
                break;
            case 3:
                numberText="ثلاث مرات";
                break;
            case 4:
                numberText="أربع مرات";
                break;
            case 7:
                numberText="سبع مرات";
                break;
            case 10:
                numberText="عشر مرات";
                break;
            case 100:
                numberText="مائة مرة";
                break;
        }
        return numberText;

    }


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

    public static  class AzkaarViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView sample, number;
        View container;
        Context ctx;
        ArrayList<AzkarForm> azkarForms = new ArrayList<AzkarForm>();
        Button tally_count,press,next;


        public AzkaarViewHolder(View itemView, Context ctx, ArrayList<AzkarForm> azkarForms) {
            super(itemView);
            this.ctx = ctx;


            //itemView.setOnClickListener(this);
            this.azkarForms = azkarForms;
            if(this.ctx instanceof MainActivity) {
                sample = (TextView) itemView.findViewById(R.id.sample);
                number = (TextView) itemView.findViewById(R.id.number);
                container = itemView.findViewById(R.id.container);
                container.setOnClickListener(this);
            }else {
                sample = (TextView) itemView.findViewById(R.id.sample);
                tally_count=(Button)itemView.findViewById(R.id.count);
                press=(Button)itemView.findViewById(R.id.press);
                press.setOnClickListener(this);
                next=(Button)itemView.findViewById(R.id.next);
            }
            //container.setOnClickListener(this);



        }


        @Override
        public void onClick(View v) {


            if (v.getId() == R.id.container){
                itemClickCallback.onItemClick(getAdapterPosition());
            } else {
                itemClickCallback.onSecondaryIconClick(getAdapterPosition());
            }

            /*if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP){
                Pair<View, String> p1 = Pair.create((View)sample, "qoute");

                ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, p1);
            }*/


        }
    }

}

第二项活动

  recyclerView =(RecyclerView) findViewById(R.id.recyclerviewcard);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        adapter=new AzkaarAdapter(AzkaarData.getArrayListData(),this);
        recyclerView.setAdapter(adapter);

        adapter.setItemClickCallback(this);

        toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

     @Override
    public void onItemClick(int p) {
        //int position=getAdapterPosition();
        AzkarForm azkarForm= list.get(p);
        Intent intent=new Intent(this,DetailsActivity.class);
        intent.putExtra("numbers",azkarForm.getNumber());
        intent.putExtra("sample",azkarForm.getSample());
        intent.putExtra("complete",azkarForm.getComplete_morning());
        intent.putExtra("position",p);
        this.startActivity(intent);
    }

    @Override
    public void onSecondaryIconClick(int p) {

    }

2 个答案:

答案 0 :(得分:0)

final Button counter = (Button) findViewById(R.id.counter_button);
counter.setText("1");

counter.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    int currNum = Integer.parseInt(counter.getText().toString());
    counter.setText("" + currNum + 1);
  }
});

根据您的布局替换按钮ID。

答案 1 :(得分:0)

好的,首先不需要你的SecondaryIconClick回调,因为你想要它做其他事并不意味着你需要其中两个,你可以设置相同的{{1}两个活动的接口,然后调用ItemCallback语句以了解从布局中单击了哪个小部件。我更容易向您展示您提供的代码, 我还没有测试过任何代码。

从我所看到的第一个switch,工作正常(我认为这不会导致问题)。除了可读性之外,我建议为Activity初始化Contextprivate Context context = this)的全局实例,然后有activity而不是new Intent(this, DetailsActivity.class),因为有时候new Intent(context, DetailsActivity.class) 'this'可以指向ClickListener而不是Activity

对于第二个Activity countersetText button的逻辑似乎很好,除非您已经在{{{{}}中初始化了Button 1}},所以你不需要在点击监听器中再次初始化它。您所要做的就是将视图传递给侦听器。 (另请参阅我对ViewHolderAdapter代码的编辑),我不知道为什么你要调用ViewHolder点击事件的新实例。

您希望点击次数在某个数字之前自动启动,还是每次点击一次直到某个数字?

无论如何,让我们为你解决这个问题。我正在编写此代码,假设您希望每次用户点击currentNum < num时计数器都会上升。

<强> FirstActivity

因此,我建议上面更改上下文引用。

R.id.press

<强> SecondActivity

 @Override
    public void onItemClick(View v, int position) {
        switch (v.getId()) {
            case R.id.container: 
             AzkarForm azkarForm= list.get(position);
             Intent intent=new Intent(context, DetailsActivity.class);
             intent.putExtra("numbers",azkarForm.getNumber());
             intent.putExtra("sample",azkarForm.getSample());
             intent.putExtra("complete",azkarForm.getComplete_morning());
             intent.putExtra("position", position);
             context.startActivity(intent);
            break;
    }

<强> RecyclerViewAdapter

您的新界面回调应为:

recyclerView =(RecyclerView) findViewById(R.id.recyclerviewcard);
    layoutManager=new LinearLayoutManager(DetailsActivity.this,LinearLayoutManager.HORIZONTAL,false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    recyclerView.scrollToPosition(pos);
    adapter=new AzkaarAdapter(AzkaarData.getArrayListData(),this);
    recyclerView.setAdapter(adapter);
    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(recyclerView);
    toolbar=(Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

}

@Override
public void onItemClick(View v, int position) {
   switch(v.getId()) {
      case R.id.press:
          AzkarForm azkarForm= list.get(position);
          int currentNum=Integer.parseInt(press.getText().toString());
          int num = azkarForm.getNumber();
              if(currentNum < num){
                 currentNum++;
              }
          v.setText(String.valueOf(currentNum));
          break;
}

<强> ViewHolder

您的新点击监听器应如下所示:

public interface ItemClickCallback {
        void onItemClick(View v, int position);
    }

public void setItemClickCallback(final ItemClickCallback itemClickCallback) {
        this.itemClickCallback = itemClickCallback;
    }

现在,您有一个干净的框架供您的听众使用,并且您传递了正确的 @Override public void onClick(View v) { if (itemClickCallback != null) { itemClickCallback.onItemClick(v, getAdapterPosition()); /*if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP){ Pair<View, String> p1 = Pair.create((View)sample, "qoute"); ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, p1); }*/ } } } 并调用了正确的views,您可以相应地应用逻辑,而不会在{{1}之间产生混淆}。

希望这有帮助。