如何在CardView中动态更改内容

时间:2017-08-15 14:12:56

标签: android android-layout android-cardview

我是Android编程的新手。我一直在尝试制作以时间,日期和描述为输入的Reminder应用程序。

我想向卡添加滑动操作。最初,卡片显示时间和日期,但当用户在卡片上滑动时,卡片中的内容必须更改,它应该完全显示一个新的布局,其中包含一个包含描述的TextView。

我到处搜索过,但他们都已经在RecyclerView中删除或移动了卡片。

我想要的是,如何使用滑动动作动态更改卡中的内容(或布局)?

这是我的卡片布局 card_view.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout `enter code here`xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="170dp">

<android.support.v7.widget.CardView
    android:layout_height="150dp"
    android:layout_width="320dp"
    android:id="@+id/card_view"
    card_view:cardCornerRadius="16dp"
    card_view:cardElevation="2dp"
    >


    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:orientation="vertical"
        android:background="@color/blue_color">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00:00"
            android:fontFamily="sans-serif-light"
            android:id="@+id/cardTime"
            android:textSize="@dimen/textview_fontsize"
            android:textColor="@color/textview_color"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DESCRIPTION"
            android:fontFamily="sans-serif-light"
            android:id="@+id/description"
            android:textSize="@dimen/textview_fontsize"
            android:textColor="@color/textview_color"
            android:visibility="gone"
            />

        <TextView
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/cardTime"
            android:layout_marginStart="12dp"
            android:layout_marginTop="17dp"
            android:id="@+id/cardPeriod"
            android:textColor="@color/textview_color"
            android:fontFamily="sans-serif-light"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/textview_color"
            android:layout_alignBottom="@+id/cardTime"
            android:id="@+id/time_line" />


        <TextView
            android:text="09/01/1997"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_marginStart="20dp"
            android:id="@+id/cardDate"
            android:textColor="@color/textview_color"
            android:fontFamily="sans-serif-light"
            android:textSize="@dimen/date_fontsize"/>


    </RelativeLayout>


</android.support.v7.widget.CardView>

MainActivity.java

public class MainActivity extends AppCompatActivity implements AlarmDialog.AlarmDialogListener {

ArrayList<CardGen> cardsList = new ArrayList<CardGen>();
RecyclerView recyclerView;
CardAdapter cardAdapter = new CardAdapter(cardsList);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);




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


    recyclerView = (RecyclerView) findViewById(R.id.cardList);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setAdapter(cardAdapter);




    ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();
            CardAdapter.CardViewHolder holder = (CardAdapter.CardViewHolder) viewHolder;

            if (direction == ItemTouchHelper.LEFT){

                    holder.timeView.setVisibility(View.GONE);
                    holder.description.setVisibility(View.VISIBLE);

            } else {

            }
        }

        @Override
        public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {


            if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){


            }
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }
    };
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    itemTouchHelper.attachToRecyclerView(recyclerView);




}


public void createAlarm(View view){

    DialogFragment dialogFragment = new AlarmDialog();
    dialogFragment.show(getFragmentManager(), "AlarmDialog");
}


@Override
public void onDialogPositiveClick(DialogFragment dialog) {
    Dialog dialog1 =  dialog.getDialog();
    TimePicker timePicker = (TimePicker) dialog1.findViewById(R.id.timePicker);
    int hour = timePicker.getHour();
    String tempHour = Integer.toString(hour);
    String minute = Integer.toString(timePicker.getMinute());
    if(minute.length()==1){minute = "0" + minute;}
    Toast.makeText(this, tempHour, Toast.LENGTH_SHORT).show();


    System.out.println("this is adapter begin");

    CardGen cardGen = new CardGen();
    if(hour>12){tempHour = Integer.toString(hour-12);}
    else if(hour==0){tempHour="12";}

    if(tempHour.length()==1){tempHour = "0"+tempHour;}
    cardGen.hour = tempHour+":"+minute;
    if(hour>=12){cardGen.period="PM";}else{cardGen.period="AM";}

    cardGen.fragmentManager = getFragmentManager();
    cardAdapter.addList(cardGen);

    System.out.println(Integer.toString(cardsList.size()));
    System.out.println("this is the middle");





    System.out.println("this is the end");

}

@Override
public void onDialogNegativeClick(DialogFragment dialog) {

}

}

RecyclerAdapter(CardAdapter.java)

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder>  {

private ArrayList<CardGen> cardsList;

public CardAdapter(ArrayList<CardGen> cardsList){
    this.cardsList = cardsList;


}

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



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

    final CardGen cardGen = cardsList.get(position);
    holder.timeView.setText(cardGen.hour);

    holder.periodView.setText(cardGen.period);
    final TextView temp = holder.dateView;
    holder.dateView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DateSelection newFragment = new DateSelection();
            newFragment.setElements(temp);


            newFragment.show(cardGen.fragmentManager, "datePicker");
        }
    });



}




@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cards, parent, false);
    return new CardViewHolder(itemView);
}


public void addList(CardGen card){
    cardsList.add(card);
    notifyItemInserted(cardsList.size());
}

public void removeList(int position){
    cardsList.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, cardsList.size());
}

public class CardViewHolder extends RecyclerView.ViewHolder{

    TextView timeView,periodView,dateView,description;
    Button transfer;

    public CardViewHolder(View v){
        super(v);

        timeView = (TextView) v.findViewById(R.id.cardTime);
        periodView = (TextView) v.findViewById(R.id.cardPeriod);
        dateView = (TextView) v.findViewById(R.id.cardDate);
        description = (TextView) v.findViewById(R.id.description);
        transfer = (Button) v.findViewById(R.id.transfer);

    }
}

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.bharath.organiserexample.MainActivity">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/cardList">


</android.support.v7.widget.RecyclerView>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="62dp"
    android:background="@null"
    android:src="@mipmap/add_button"
    android:layout_gravity="bottom|right"
    android:onClick="createAlarm" />


</FrameLayout>

2 个答案:

答案 0 :(得分:0)

可能您应该使用数据库,然后添加滑动侦听器或(手势检测器)。每当刷卡时,您从卡中删除当前项目,然后从数据库或列表中获取新项目。但我认为最好用列表之类的东西,以便你可以跟踪当前项目。并且还能够轻松删除刷过的物品。当用户添加项目时,它将从数据库添加到列表中。这样你就不必担心下一个项目的位置了。

答案 1 :(得分:0)

有几种选择。最简单的方法是让您的cardview包含您的描述,但最初将可见性设置为GONE,然后当滑动事件发生时,将初始状态反转为GONE,将描述反转为VISIBLE。我会创建一个帮助方法来执行此操作。至于捕捉滑动手势,请看一下这个答案。

Swipe to Dismiss for RecyclerView

您需要使用recyclerview,但如果您将反转视图逻辑放在onSwiped方法中,这应该可以使用

修改 要获取cardview,请使用recyclerview中的ViewHolder

PropertyChangedCallback