Android:如何从MainActivity

时间:2016-03-22 08:00:39

标签: android android-recyclerview

我有使用Textview和Checkbox的Recyclerview。每个项目都可以隐藏或显示。在MainActivity中,在运行时我只显示未隐藏的项目。但在菜单中我有一个名为 - 显示隐藏项目的选项。点击我还有隐藏的项目,但灰色。 我该怎么做?如何从MainActivity达到Recyclerview的单项物业?

这是我的代码:

RVAdapter

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.DataHolder> {

    static String LOG = "RV_LOG";
    ArrayList<DataObject> myData;
    MainActivity mainActivity;
    public static MyClickListener myClickListener;

    public class DataHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
            CompoundButton.OnCheckedChangeListener {
        TextView text;
        CheckBox checkBox;
        private int position;
        public DataHolder(View itemView) {
            super(itemView);
            text = (TextView) itemView.findViewById(R.id.text);
            checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);

            checkBox.setOnCheckedChangeListener(this);
        }

        public void setPosition(int value) {
            this.position = value;
        }


        @Override
        public void onClick(View v) {
            myClickListener.onItemClick(position, v);
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            mainActivity.onItemClick(position, buttonView, isChecked);

        }
    }

    public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public RVAdapter(ArrayList<DataObject> myData, MainActivity mainActivity) {
        this.myData = myData;
        this.mainActivity=mainActivity;
    }

    @Override
    public DataHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_single_item, parent, false);

        return new DataHolder(view);
    }


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

        holder.text.setText(myData.get(position).getText());
        holder.checkBox.setChecked(myData.get(position).getCheck());
        if (myData.get(position).getHidden()) {
            holder.text.setTextColor(Color.parseColor("#d6d6c2"));
            holder.text.setVisibility(View.GONE);
            holder.checkBox.setVisibility(View.GONE);
        }
        holder.setPosition(position);
    }


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    public void addItem(DataObject object, int index) {
        myData.add(index, object);
        notifyItemInserted(index);
    }

    public void deleteItem(int index) {
        myData.remove(index);
        notifyItemRemoved(index);
    }

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

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

MainActivity:

public class MainActivity extends AppCompatActivity {

    private RecyclerView myRecyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager manager;


    private ArrayList<DataObject> data;
    String TAG = "MA Tag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        data = new ArrayList<DataObject>();
        boolean hidden = true;
        for (int i = 0; i < 10; i++) {
            DataObject object = new DataObject("Item No " + (i + 1), false, hidden);
            data.add(i, object);
            hidden = !hidden;
        }

        myRecyclerView = (RecyclerView) findViewById(R.id.rv);
        myRecyclerView.setHasFixedSize(true);
        adapter = new RVAdapter(data, this);
        myRecyclerView.setAdapter(adapter);
        manager = new LinearLayoutManager(this);
        myRecyclerView.setLayoutManager(manager);

    }

    public void onItemClick(int position, View view, boolean isChecked) {
        switch (view.getId()) {
            case R.id.text:
                Log.d(TAG, "item is clicked");
                break;

            case R.id.checkbox:
                data.get(position).setCheck(isChecked);
                Log.d(TAG, position + " is " + data.get(position).getCheck() + ":" + isChecked);
                if (isChecked) {
                    data.get(position).setCheck(true);
               } else {
                    data.get(position).setCheck(false);
                    Log.d(TAG, position + " is " + data.get(position).getCheck());

                }
                break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.delete:

               for(int i = data.size()-1; i >=0; i--){
                   if(data.get(i).getCheck())
                        ((RVAdapter) myRecyclerView.getAdapter()).deleteItem(data.indexOf(data.get(i)));
                   }

                break;
            case R.id.show:
                Log.d(TAG, "Show");
                for(int i = 0; i <data.size(); i++){
                    if(data.get(i).getHidden()){
                        Log.d(TAG, data.get(i).getText());
                        data.get(i).setHidden(false);
                    }
                }

                break;

        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.layout_menu, menu);
        return true;
    }
}

更新:

现在我的RVAdapter看起来如此:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.DataHolder> {

    static String LOG = "RV_LOG";
    ArrayList<DataObject> myData;
    MainActivity mainActivity;

    public class DataHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
            CompoundButton.OnCheckedChangeListener, View.OnLongClickListener {
        TextView text;
        CheckBox checkBox;
        private int position;
        public DataHolder(View itemView) {
            super(itemView);
            text = (TextView) itemView.findViewById(R.id.text);
            checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);

            checkBox.setOnCheckedChangeListener(this);
            text.setOnLongClickListener(this);
        }

        public void setPosition(int value) {
            this.position = value;
        }

        @Override
        public void onClick(View v) {
            mainActivity.onItemClick(position, v, true);
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mainActivity.onItemClick(position, buttonView, isChecked);
        }

        @Override
        public boolean onLongClick(View v) {
            mainActivity.onLongClick(position);
            return true;
        }
    }

    public RVAdapter(ArrayList<DataObject> myData, MainActivity mainActivity) {
        this.myData = myData;
        this.mainActivity=mainActivity;
    }

    @Override
    public DataHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_single_item, parent, false);

        return new DataHolder(view);
    }

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

        holder.text.setText(myData.get(position).getText());
        holder.checkBox.setChecked(myData.get(position).getCheck());
        if (myData.get(position).getHidden()) {
            holder.text.setVisibility(View.GONE);
            holder.checkBox.setVisibility(View.GONE);
        }

        if(myData.get(position).getWasHidden()){
            holder.text.setTextColor(Color.parseColor("#666666"));
        }
        holder.setPosition(position);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    public void deleteItem(int index) {
        myData.remove(index);
        notifyItemRemoved(index);
        notifyItemRangeChanged(index, myData.size());
    }

    public void hideItem(int position){
        myData.get(position).setHidden(true);

    }

    public void showItem(int position){
        myData.get(position).setHidden(false);
        notifyDataSetChanged();
    }

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

在我的Activity中我调用了showItem()方法。

  public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.delete:
               for(int i = data.size()-1; i >=0; i--){
                   if(data.get(i).getCheck())
                        ((RVAdapter) myRecyclerView.getAdapter()).deleteItem(data.indexOf(data.get(i)));
                   }
                break;
            case R.id.show:
                for(int i = 0; i< data.size(); i++) {
                    if(data.get(i).getHidden())
                    ((RVAdapter) myRecyclerView.getAdapter()).showItem(i);
                }
                adapter.notifyDataSetChanged();
                break;
        }
        return true;
    }

它有点错误:在运行时每秒显示一个项目(正确,因为每秒都没有隐藏)。当我点击显示时,隐藏的项目会出现,但前两个显示的项目会消失。当我再次点击Show时,它们会出现。这有什么不对?

1 个答案:

答案 0 :(得分:1)

在适配器内创建一个方法以显示隐藏的项目。由于您在Main Activity中有适配器对象,因此您可以从Main Activity中轻松调用该方法。调用该方法后,从MainActivity本身调用notifyDataSetChanged()方法。 notifyDataSetChanged()方法将由适配器对象调用。