android ListView实现圆角(两种解决方案)

android ListView实现圆角(两种解决方案)首先呢,我们还是看几个示图:(这是360推出的一款天气预报APP(墨迹),很不错的一款哦,这里为她们做一个免费广告,哈哈.)          这种带有圆角的listview’看起来很棒吧,确实是这样,其实也不能这么说,主要方形太多了,斯通见惯就不值钱了,“物以稀为贵嘛”.就好比学java都搞androd,很明显嘛,为了多赚点钱,可是供过于求的话,就不这么乐观了,就好比现在

大家好,又见面了,我是你们的朋友全栈君。

首先呢,我们还是看几个示图:(这是360推出的一款天气预报APP(墨迹),很不错的一款哦,这里为她们做一个免费广告,哈哈.)

 android ListView实现圆角(两种解决方案)       android ListView实现圆角(两种解决方案)        android ListView实现圆角(两种解决方案)

这种带有圆角的listview' 看起来很棒吧,确实是这样,其实也不能这么说,主要方形太多了,斯通见惯就不值钱了,“物以稀为贵嘛”. 就好比学java都搞androd,很明显嘛,为了多赚点钱,可是供过于求的话,就不这么乐观了,就好比现在这个圆角,如果太多太多的话,我想若干时间段,肯定会被新的视图所代替.所以“跟随潮流,放宽眼线”很重要.不扯了,(网上实现例子很多)下面简单介绍下实现方法:(两种方法)

第一种:我们通过配置文件也就是shape自己实现圆角的背景,以及selector选中某项的背景.

首先我们要实现一个完整的圆角背景,用于默认与listview的background.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <stroke
        android:width="1dp"
        android:color="@color/gray" />

    <solid android:color="@color/white" />

    <corners android:radius="8dp" />

</shape>

接下来我们要实现也就是listview的第一项selector后背景的shape.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <stroke
        android:width="1dp"
        android:color="@color/gray" />

    <solid android:color="@color/gray" />

    <corners
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />

</shape>

下面呢,我们也要实现中间的shape,以及末尾的shape等等配置.这里就不写了.按照上面模仿就ok了.

第二种方法:我们用.9.png完全代替上面那些配置,(因为.9.png可以拉伸不毁容

所以listview的第一项,中间项,最后一项,以及就一项,我们都可以通过图片来实现.

这里我运用第一种方法:(也巩固下shape)

创建自定义的listview,用于实现setSelector及选中的效果.

   代码片段:

   

package com.jj.listview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.AdapterView;
import android.widget.ListView;

/***
 * 自定义listview
 * 
 * @author Administrator
 * 
 */
public class MyListView extends ListView {
	public MyListView(Context context) {
		super(context);
	}

	public MyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/****
	 * 拦截触摸事件
	 */
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			int x = (int) ev.getX();
			int y = (int) ev.getY();
			int itemnum = pointToPosition(x, y);
			if (itemnum == AdapterView.INVALID_POSITION)
				break;
			else {
				if (itemnum == 0) {
					if (itemnum == (getAdapter().getCount() - 1)) {
						// 只有一项
						setSelector(R.drawable.list_round);
					} else {
						// 第一项
						setSelector(R.drawable.list_top_round);
					}
				} else if (itemnum == (getAdapter().getCount() - 1))
					// 最后一项
					setSelector(R.drawable.list_bottom_round);
				else {
					// 中间项
					setSelector(R.drawable.list_center_round);
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			break;
		}
		return super.onInterceptTouchEvent(ev);
	}
}

这段代码网上很多,几乎全部都是这么实现的,这里我简单介绍,如果说错了,请大家指出,

首先我们是实现了onInterceptTouchEvent这个方法,在这里我们也可以用onTouchEvent事件,都可以实现我们想要的效果.

onInterceptTouchEvent和onTouchEvent的区别:简单的来说前者可以拦截后者.

详细的请大家参考http://blog.csdn.net/jj120522/article/details/7944916  强烈建议大家看看.

下面一些逻辑虽说没有见过,我想大家都看得懂,(就是获取坐标(x,y),然后根据坐标获取listview相应的position值,没有返回-1,然后根据相应的position设置相应的setSelector ).有时间得好好研究下listview.对灵活运用很有帮助.


在这里我还要在说名一点,上面那个图形很显然不是一个listview,是三个listview,另外重要的是我们一个屏幕显示不完全,这时我们就用到了ScrollView,一提到这个我想大家都知道ScrollView和listview是冤家,不可能同时存在,不过网上有解决办法,原理就是我们动态show 我们的listview,

实现方法:

/***
	 * 动态设置listview的高度
	 * 
	 * @param listView
	 */
	public void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		// params.height += 5;// if without this statement,the listview will be
		// a
		// little short
		// listView.getDividerHeight()获取子项间分隔符占用的高度
		// params.height最后得到整个ListView完整显示需要的高度
		listView.setLayoutParams(params);
	}

我们在Listview的setAdapter后,在调用下这个方法就OK了,代码内容,我想大家都看的明白,就不多介绍了.

效果图:

android ListView实现圆角(两种解决方案)     android ListView实现圆角(两种解决方案)    android ListView实现圆角(两种解决方案)

样子虽丑陋了点,但是实现效果就行了,如果在项目中就另当别论了。

哈哈,实现起来简单吧,睡觉去.


下面介绍另外一种解决方法:(TableLayout)

/************************************************************************************************************************************/

对于简短的listview,加上如上面这些比较死的数据,我们完全没有必要用listview来实现,TableLout完全够了.主要是我们怎么实现带有弧度的边罢了,不过这也完全得力于shape的功劳,至于点击效果效果嘛,我们有selector,哈哈,下面介绍下实现:

 配置文件部分代码:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none" >

        <LinearLayout
            android:id="@+id/ll_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" >
        </LinearLayout>
    </ScrollView>

很简单,我们主要用这个Linerlayout来呈放我们的Table.

实现代码:

package com.jj.corner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.R.color;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity2 extends Activity {
	private LinearLayout ll_main;
	private TableLayout tableLayout;

	private LinearLayout.LayoutParams layoutParams;

	private static final String MSG_0[] = { "jjhappyforever" };

	private static final String MSG_1[] = { "天气动画", "通知栏天气" };

	private static final String MSG_2[] = { "桌面插件", "绑定微博", "天气分享", "通知与提示",
			"定时播报" };

	private static final String MSG_3[] = { "检查新版本", "发送建议", "帮助", "关于" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main2);
		ll_main = (LinearLayout) findViewById(R.id.ll_main);
		showTable();
	}

	/***
	 * 显示table
	 */
	public void showTable() {

		layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		layoutParams.bottomMargin = 30;
		layoutParams.topMargin = 10;
		ll_main.addView(getTable(MSG_0), layoutParams);

		ll_main.addView(getTable(MSG_1), layoutParams);

		ll_main.addView(getTable(MSG_2), layoutParams);

		ll_main.addView(getTable(MSG_3), layoutParams);
	}

	/***
	 * 获取Table
	 * 
	 * @param array
	 * @return
	 */
	public TableLayout getTable(String[] array) {

		tableLayout = new TableLayout(this);
		tableLayout.setLayoutParams(layoutParams);
		tableLayout.setStretchAllColumns(true);

		for (int i = 0; i < array.length; i++) {
			TableRow tableRow = new TableRow(this);
			View view = getView(array[i], i, array.length);
			tableRow.addView(view);

			tableLayout.addView(tableRow);

		}
		return tableLayout;

	}

	/****
	 * 
	 * @param msg
	 *            显示信息
	 * @param current_Id
	 *            当前个数
	 * @param totle_Num
	 *            总个数
	 * @return
	 */
	public View getView(String msg, int current_Id, int totle_Num) {

		LinearLayout linearLayout = new LinearLayout(this);

		LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		layoutParams1.height = 1;
		linearLayout.setOrientation(1);
		// 创建分割线
		View line = new View(this);
		line.setLayoutParams(layoutParams1);
		line.setBackgroundColor(getResources().getColor(R.color.black));

		View view = LayoutInflater.from(MainActivity2.this).inflate(
				R.layout.item, null);
		view.setBackgroundDrawable(new BitmapDrawable());

		view.setFocusable(true);
		view.setClickable(true);
		TextView textView = (TextView) view.findViewById(R.id.tv_list_item);
		textView.setText(msg);
		textView.setTextSize(20);

		// 只有一项
		if (totle_Num == 1) {
			view.setBackgroundResource(R.drawable.default_selector);
			return view;
		}
		// 第一项
		else if (current_Id == 0) {
			view.setBackgroundResource(R.drawable.list_top_selector);
		}
		// 最后一项
		else if (current_Id == totle_Num - 1) {
			view.setBackgroundResource(R.drawable.list_bottom_selector);
			line.setVisibility(View.GONE);
		} else
			view.setBackgroundResource(R.drawable.list_center_selector);

		linearLayout.addView(view);
		linearLayout.addView(line);

		return linearLayout;
	}
}

主要是getView有些复杂,不过都很好理解,相信大家都可以明白,只是没有动手做而已,小弟我只是想用这个练练手.

不过不要忘记我们的背景实现

这个是只有一项的背景,

default_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"><shape>
            <stroke android:width="1dp" android:color="@color/gray" />

            <solid android:color="@color/gray" />

            <corners android:radius="8dp" />
        </shape></item>
    <item><shape>
            <stroke android:width="1dp" android:color="@color/gray" />

            <solid android:color="@color/white" />

            <corners android:radius="8dp" />
        </shape></item>

</selector>

list_top_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"><shape>
            <stroke android:width="1dp" android:color="@color/gray" />

            <solid android:color="@color/gray" />

            <corners android:topLeftRadius="8dp" android:topRightRadius="8dp" />
        </shape></item>
    <item><shape>

            <solid android:color="@color/white" />

            <corners android:topLeftRadius="8dp" android:topRightRadius="8dp" />
        </shape></item>

</selector>

list_bottom_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"><shape>
            <stroke android:width="1dp" android:color="@color/gray" />

            <solid android:color="@color/gray" />

            <corners android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp" />
        </shape></item>
    <item><shape>

            <solid android:color="@color/white" />

            <corners android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp" />
        </shape></item>

</selector>

list_center_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"><shape>
            <stroke android:width="1dp" android:color="@color/gray" />

            <solid android:color="@color/gray" />
        </shape></item>
    <item><shape>
            <solid android:color="@color/white" />
        </shape></item>

</selector>

主要就这么多了,下面我们来看下实现效果:

android ListView实现圆角(两种解决方案)    android ListView实现圆角(两种解决方案)     android ListView实现圆角(两种解决方案) 

怎么样,效果和上面一样吧,自我感觉这样实现比上面方面点,不过最重要的是因人而异,达成目的就OK了.

有不足的地方请留言指出.

 源码下载


    由于比较忙碌,顾不得大家一一发送,现将代码上传网上,如有需要,你可以下载查看.

     对你如有帮助,记得赞一个!

     Thranks for you !

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160586.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mac redis客户端_redis关闭服务器命令

    mac redis客户端_redis关闭服务器命令我选择的是直接使用Mac的Homebrew工具安装redis,可以节省很多配置的时间。1.安装命令brewinstallredis2.使用配置文件启动redis$redis-server或brewservicesstartredis3.连接远程服务器的数据库$redis-cli-hhost-pport-apassword//eg:$redis-cli-h192.168.11.225-p6379-a“password”//eg:$redis-cli

    2025年11月30日
    7
  • js判断是否包含指定字符串_js判断字符串是否相等

    js判断是否包含指定字符串_js判断字符串是否相等js判断字符串是否包含某个字符串方法一:includes方法//str.includes(“”)返回一个布尔值,值为true时表示包含varstr=”helloworld”;if(str.includes(“world”)){alert(“Hi,world”);}方法二:indexOf方法varstr=”123456″;if(str.indexOf(“5”)!=-1){console.log(“字符串123456里包含了5”);

    2022年10月6日
    5
  • 操作系统的详细讲解(3.1)

    操作系统的详细讲解(3.1)

    2021年11月11日
    147
  • JAVA中String的深入研究

    每次上网冲杯Java时,都能看到关于String无休无止的争论。还是觉得有必要让这个讨厌又很可爱的String美眉,赤裸裸的站在我们这些Java色狼面前了。嘿嘿….众所周知,String是由字符组成的串,在程序中使用频率很高。Java中的String是一个类,而并非基本数据类型。 不过她却不是普通的类哦!!! 【镜头1】 String对象的创建       1、关于

    2022年3月11日
    44
  • Endnote 域代码已更改

    Endnote 域代码已更改word中参考文献位置出现“域代码已更改”的批注,并且还没有办法删除,现提供如下两个可能可以的解决方法:1.Alt+F9,确实会显示域代码,但是无法解决我的问题;2.直接换一个endnote格式,这个倒是解决了我的问题;上面两种方法都是网上找到的,仅做参考,希望在读者苦苦找寻而不得解的时候,能给予一点点及时的帮助,也希望以后我用得上的时候,我自己也能看到goodluck…

    2022年6月7日
    133
  • jdk 8 stream_stream流是什么

    jdk 8 stream_stream流是什么Java8新特性:Stream流详解本文章转载自头条网,只是觉得好用很详细,所以自己收集做下笔记,不做任何商业用途,不收任何费用,不喜勿喷。本文是转载,希望不要涉及到文章版权,只是自己做笔记。_________这个是最重要的。致敬头条@程序猿的内心独白1.Stream初体验我们先来看看Java里面是怎么定义Stream的:Asequenceofele…

    2022年10月6日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号