java - 构建倒计时器的 GridView 时出现问题

标签 java android gridview timer countdowntimer

所以我有一个网格布局,

activity_main.xml

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:verticalSpacing="0dp"
        android:horizontalSpacing="0dp"
        android:stretchMode="columnWidth"
        android:numColumns="4"
        android:clickable="false" />
</FrameLayout>

在该布局内部,我为网格单元指定了格式

grid_item.xml

<com.timer.app.ImageView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    />
<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    android:layout_gravity="bottom"
    android:textColor="@android:color/white"
    android:background="#55000000"
    />

我试图实现的目标是在每次单击网格项时,设置其适当的 TextView 以显示我设置的时间的倒计时。

这就是我陷入困境的地方:我不知道如何创建与每个网格项相关的倒计时器,以及如何检测哪个网格项被推送。

我知道常见的响应可能是指向 CountDownTimer 文档的链接,我已阅读该文档,但没有向我解释如何创建可以与每个网格项相关的计时器数组。

最佳答案

我不确定我可以帮助倒计时器,但我知道如何注册已单击的网格单元格。过去,我通过将 GridView 的每个单元格设置为一个按钮,并在创建每个单元格时向每个单元格添加一个标签来完成此操作。

日历.xml

<GridView
    android:id="@+id/calendar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:horizontalSpacing="-1px"
    android:numColumns="7"
    android:verticalSpacing="-1px" >
</GridView>

gridcell.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/gridcell"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="@color/black"
        android:background="@drawable/calendar_button_selector_blue"
        android:textAppearance="?android:attr/textAppearanceMedium">
    </Button>
</LinearLayout>

然后,在java中:

GridView calendarView = (GridView) this.findViewById(R.id.calendar);
GridCellAdapter adapter = new GridCellAdapter(getApplicationContext(), R.id.gridcell);
adapter.notifyDataSetChanged();
calendarView.setAdapter(adapter);

public class GridCellAdapter extends BaseAdapter implements OnClickListener {
    public GridCellAdapter(Context context, int textViewResourceId) {
        super();
        // ...
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                // ROW INFLATION
                LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.gridcell, parent, false);
            }
            gridcell = (Button) row.findViewById(R.id.gridcell);
            gridcell.setOnClickListener(this);
            gridcell.setTag(R.id.TAG_ONE, 1);
    }

    @Override
        public void onClick(View view) {
            int value = (Integer) view.getTag(R.id.TAG_ONE);
    }

}

关于java - 构建倒计时器的 GridView 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23042333/

相关文章:

c# - 使用接口(interface)将行添加到 XtraGrid

javascript - Yii2:在单击按钮时更改 Gridviews 的 DataProvider

java - 如何处理创建的损坏文件但发生 IOException?

java - 查找步行道距离之间的高度

java - Java 中有泛型枚举吗?

java - 为什么 AES/CTR/NoPadding 坏了?

java - Struts 2 操作错误 - 无法继续到下一页

android - 如何将每个 viewpager 页面位置存储在它们的列表中?

android - 窗口标题文字大小

android爆炸过渡不会爆炸GridView