Android Custom ListView。单击后显示不同的行布局

时间:2017-01-07 22:39:44

标签: android android-layout listview

我是Android工作室的新手,我面对这个问题两个小时。正在寻找其他答案和其他网站的解决方案没有任何结果。 我有ListView的片段,其中包含一些要执行的任务。所有任务都存储在数据库中,我使用Custom CursorAdapter来表示这些数据。 我有两个不同的row_layout,一个是只有图像,任务标题和截止日期。第二个具有相同的东西,两个按钮和单个任务的描述。单击ListView中的单行后,用户应该看到这些附加按钮和TextView。我已经尝试过,但我迷路了。

这是适配器代码:

public class ChallengeAdapter extends CursorAdapter {

private int position = 1;

public static class ChallengeViewHolder {

    ImageView challengeIcon;
    TextView challengeTitle;
    TextView challengeDeadline;
    TextView challengeDescription;
    Button bFinished;
    Button bRemove;

}

public static final int[] challengeIcons = {

        R.drawable.brain_icon,
        R.drawable.book_icon,
        R.drawable.workout_icon
};

public ChallengeAdapter(Context context, Cursor cursor) {

    super(context, cursor, 0);
}

public void selectedItem(int position) {

    this.position = position;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {


    ChallengeViewHolder holder = new ChallengeViewHolder();

    if(this.position == cursor.getPosition()) {

        View view2 = LayoutInflater.from(context).inflate(R.layout.extended_row_challenge_list, parent, false);
        holder.challengeIcon = (ImageView) view2.findViewById(R.id.ChallengeIconExtended);
        holder.challengeTitle = (TextView) view2.findViewById(R.id.ChallengeTitleExtended);
        holder.challengeDeadline = (TextView) view2.findViewById(R.id.ChallengeDeadlineExtended);
        holder.challengeDescription = (TextView) view2.findViewById(R.id.ChallengeDescriptionExtended);
        holder.bFinished = (Button) view2.findViewById(R.id.bFinished);
        holder.bRemove = (Button) view2.findViewById(R.id.bRemove);

        view2.setTag(holder);

        return view2;
    }


    View view = LayoutInflater.from(context).inflate(R.layout.row_challenge_list, parent, false);
    holder.challengeIcon = (ImageView) view.findViewById(R.id.ChallengeIcon);
    holder.challengeTitle = (TextView) view.findViewById(R.id.ChallengeTitle);
    holder.challengeDeadline = (TextView) view.findViewById(R.id.ChallengeDeadline);

    view.setTag(holder);

    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    ChallengeViewHolder holder = (ChallengeViewHolder) view.getTag();

    holder.challengeTitle.setText(cursor.getString(cursor.getColumnIndexOrThrow("title")));
    holder.challengeDeadline.setText(cursor.getString(cursor.getColumnIndexOrThrow("deadline")));
    holder.challengeIcon.setImageResource(challengeIcons[cursor.getInt(cursor.getColumnIndexOrThrow("icon_id"))]);
}

}

以下是单行代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/normalRowLayout"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/ChallengeIcon"
    android:layout_width="80dp"
    android:layout_height="80dp"
    />

<TextView
    android:id="@+id/ChallengeTitle"
    android:layout_width="270dp"
    android:layout_height="25dp"
    android:textStyle="bold"
    android:textSize="18sp"
    android:text="HERE COMES THE BASS!"
    android:layout_toRightOf="@+id/ChallengeIcon"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:textColor="#3696E4"
    />


<TextView
    android:id="@+id/ChallengeDeadline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:text="28 days 6 hours 7 minutes 16 secunds"
    android:layout_toRightOf="@+id/ChallengeIcon"
    android:layout_marginLeft="15dp"
    android:layout_below="@+id/ChallengeTitle"
    android:layout_marginTop="15dp"
    android:textColor="#FF0000"
    />

和扩展行:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/extendedRowLayout"
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView
    android:id="@+id/ChallengeIconExtended"
    android:layout_width="80dp"
    android:layout_height="80dp"
    />

<TextView
    android:id="@+id/ChallengeTitleExtended"
    android:layout_width="270dp"
    android:layout_height="25dp"
    android:textStyle="bold"
    android:textSize="18sp"
    android:text="HERE COMES THE BASS!"
    android:layout_toRightOf="@+id/ChallengeIconExtended"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="15dp"
    android:textColor="#3696E4"
    />

<TextView
    android:id="@+id/ChallengeDescriptionExtended"
    android:layout_width="290dp"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:layout_toRightOf="@+id/ChallengeIconExtended"
    android:layout_marginLeft="5dp"
    android:layout_below="@+id/ChallengeTitleExtended"
    android:layout_marginTop="15dp"
    android:text="description description description description description description description description description description description description vdescription description description description description description description"
    android:textColor="#000000"
    />


<TextView
    android:id="@+id/ChallengeDeadlineExtended"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:text="28 days 6 hours 7 minutes 16 secunds"
    android:layout_toRightOf="@+id/ChallengeIconExtended"
    android:layout_marginLeft="25dp"
    android:layout_below="@+id/ChallengeDescriptionExtended"
    android:layout_marginTop="15dp"
    android:textColor="#FD1103"
    />

<Button
    android:id="@+id/bFinished"
    android:layout_width="150dp"
    android:layout_height="50dp"
    android:layout_below="@+id/ChallengeDeadlineExtended"
    android:layout_marginTop="10dp"
    android:text="Finished"
    android:background="@drawable/roundedbuttongreen"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="20dp"
    />

<Button
    android:id="@+id/bRemove"
    android:layout_width="150dp"
    android:layout_height="50dp"
    android:layout_below="@+id/ChallengeDeadlineExtended"
    android:layout_marginTop="10dp"
    android:text="Remove"
    android:background="@drawable/roundedbuttonblue"
    android:layout_alignParentRight="true"
    android:layout_marginRight="20dp"
    />

当我在适配器中将位置设置为1时,还添加了Photo的样子:

enter image description here

0 个答案:

没有答案