android中gridview的item为啥不能居中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android中gridview的item为啥不能居中相关的知识,希望对你有一定的参考价值。

设置gridview的属性中的子布局文件里面,加上android:gravity="center"

<RelativeLayout xmlns:android="

    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="#f0f0f0"
        android:horizontalSpacing="4dp"
        android:numColumns="3"
        android:verticalSpacing="4dip">
 
    </GridView>
 
</RelativeLayout>
 
 
<?xml version="1.0" encoding="utf-8"?>
 
<RelativeLayout xmlns:android="

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
 
    <ImageView
        android:id="@+id/grid_view_item_img"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop" />
 
</RelativeLayout>

参考技术A 设置gridview的属性中的子布局文件里面,加上android:gravity="center"本回答被提问者和网友采纳

android实现ListView或GridView中item的倒数功能

有时在开发中可能会遇到当未到某个时间时需要让ListViewGridView中的相应item进行倒数的需求,此时,如果还是按照常见的ListViewGridViewAdapter的写法+计时功能来实现的话,则当滚动ListViewGridView的时候,item就可能会出现复用问题(即不需要进行倒数或当前item的倒数已经结束了,却还会出现倒数的现象),因此,为了解决这种问题,需要进行倒数时,重新遍历当前对象的集合,然后让每个对象的相应时间值减1,最后再通过AdapternotifyDataSetChanged方法静态的刷新界面。

  效果如下所示:

MainActivity的代码如下所示:

<span style="font-size:14px;">package com.abner.countdownforlistview;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity 

    private ListView lv_goods;
    private GoodsAdapter adapter;

    private int result = -1;
    private Handler handler = new Handler() 
        @Override
        public void handleMessage(Message msg) 
            super.handleMessage(msg);
            if (msg.what == 1) 
                result = adapter.countDownTime();
                if (result == 0) 
                    handler.removeMessages(1);
                    return;
                 else 
                    adapter.notifyDataSetChanged();
                    handler.sendEmptyMessageDelayed(1, 1000);
                
            
        
    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_goods = (ListView) findViewById(R.id.lv_goods);
        initData();
    

    private void initData() 
        List<Goods> goodsList = new ArrayList<>();
        for (int i = 0; i < 15; i++) 
            Goods goods = new Goods();
            goods.setGoodsName("这是商品名称:" + i);
            goods.setPrice(i * 26);
            goods.setTime(i % 3 == 0 ? i * 8 + 62 : 0);
            goodsList.add(goods);
        
        adapter = new GoodsAdapter(this, goodsList);
        lv_goods.setAdapter(adapter);
        handler.sendEmptyMessageDelayed(1, 1000);
    

</span>

在这个MainActivity中,可以看到通过Handler实现了倒数功能,在为ListView设置完Adapter后即通过handlersendEmptyMessageDelayed方法发送了一条消息,然后在handlerhandleMessage方法中通过调用在adapter中声明的countDownTime方法对相应的对象集合进行遍历再设置相应的值,最后通过返回的result进行判断倒数是否已经结束,如果已经结束则移除消息,否则继续发送消息进行倒数操作

ListViewGoodsAdapter的代码如下所示:

<span style="font-size:14px;">package com.abner.countdownforlistview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import static com.abner.countdownforlistview.R.id.tv_price;

/**
 * Created by abner on 2016/5/6.
 */
public class GoodsAdapter extends BaseAdapter 
    private Context context;
    private List<Goods> goodsList;

    public GoodsAdapter(Context context, List<Goods> goodsList) 
        this.context = context;
        this.goodsList = goodsList;
    

    @Override
    public int getCount() 
        return goodsList == null ? 0 : goodsList.size();
    

    @Override
    public Object getItem(int position) 
        return goodsList.get(position);
    

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
        final ViewHolder holder;
        if (convertView == null) 
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.lv_goods_item, null);
            holder.tv_goodsName = (TextView) convertView.findViewById(R.id.tv_goodsName);
            holder.tv_price = (TextView) convertView.findViewById(tv_price);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            convertView.setTag(holder);
         else 
            holder = (ViewHolder) convertView.getTag();
        

        final Goods goods = goodsList.get(position);
        holder.tv_goodsName.setText(goods.getGoodsName());
        holder.tv_price.setText("¥" + goods.getPrice());

        final int time = goods.getTime();
        if (time > 0) 
            holder.tv_time.setText(getTimeStr(time) + "后开始");
         else 
            holder.tv_time.setText("正在进行中...");
        

        return convertView;
    

    class ViewHolder 
        TextView tv_goodsName;
        TextView tv_price;
        TextView tv_time;
    


    private int result = -1;

    /**
     * 倒数,实时更新每个item的显示时间
     *
     * @return
     */
    public int countDownTime() 
        new Thread(new Runnable() 
            @Override
            public void run() 
                try 
                    Thread.sleep(1000);
                    //如果goodsList为null或者result等于goodsList.size+1,则表示已经倒数完,此时需要停止倒数
                    if (goodsList == null || result == goodsList.size() + 1) 
                        result = 0;
                        return;
                    
                    result = 1;
                    for (Goods goods : goodsList) 
                        if (goods.getTime() == 0) 
                            goods.setTime(0);
                            result++;   //每当一个对象倒数完或者当前对象已经处于进行中,则让result加1,用于后续所有对象倒数完后,停止倒数
                         else 
                            goods.setTime(goods.getTime() - 1);
                        
                    
                 catch (InterruptedException e) 
                    e.printStackTrace();
                
            
        ).start();
        return result;
    

    /**
     * 格式化时间显示,格式为:XX分XX秒
     *
     * @param timeDiff
     * @return
     */
    public static String getTimeStr(int timeDiff) 
        int min = timeDiff / 60;
        int sec = timeDiff - min * 60;

        String result = "";
        if (min == 0) 
            result = sec + "秒";
         else 
            result = min + "分" + sec + "秒";
        

        return result;
    
</span>

此时,在android中实现ListViewGridViewitem的倒数功能就已经完成了。

 源码下载

 

 

以上是关于android中gridview的item为啥不能居中的主要内容,如果未能解决你的问题,请参考以下文章

如何设置 android gridview 怎么设置 , 一些item可以点击,另外.....

android实现ListView或GridView中item的倒数功能

android实现ListView或GridView中item的倒数功能

android如何删除gridview中的某个item

Android 中 Animation 怎么停止

android gridview 如何点击item中的一个组合控件的图标按钮触发整个item