ListView里面的自定义控件

       刚开始学的时候,发现网上给的demo都会缺少XML的。。

其实也没有必要放,可能你到了一定的境界觉得别人都会理解。

可是我作为新手,有时候却不知道少了些什么,哎,所以挺理解这种痛苦的吧。

我尽量代码补全一点吧。。  希望写错的地方大家不要介意。




前面有两个XML的布局,一个是listview的,一个是listView里面的item的布局,也就是一条一条的那个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="blue.boki.com.emojidemo.ApplyActivity">

    <ListView
        android:id="@+id/apply_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:paddingLeft="30dp"
  >

    <ImageView
        android:id="@+id/imagApply"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="10dp"
        />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp "
        android:orientation="vertical"
        android:layout_marginLeft="20dp"
        android:layout_gravity="center_vertical"
        >

        <TextView
            android:id="@+id/t1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            />
        <TextView
            android:id="@+id/t2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageButton
            android:id="@+id/imagButton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@android:color/black"
            android:layout_gravity="right"
            android:layout_marginRight="10dp"
            />

    </FrameLayout>


</LinearLayout>


接下来是mainActivity的逻辑,很简单的一个adapter,然后数据我是写死的。。毕竟是个demo嘛。。。
大家先学会定义自定义控件,以后也就好换JSON数据拉就是的了

<pre name="code" class="java">public class ApplyActivity extends Activity {

private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apply);

list = (ListView) findViewById(R.id.apply_listview);

ArrayList<HashMap<String,String>> array = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map = new HashMap<String,String>();
HashMap<String,String> map1 = new HashMap<String,String>();
HashMap<String,String> map2 = new HashMap<String,String>();
HashMap<String,String> map3 = new HashMap<String,String>();
HashMap<String,String> map4 = new HashMap<String,String>();


map.put("t1","小恩爱");
map.put("t2","恋爱神器,只与心爱的人");
map1.put("t1","恋爱记");
map1.put("t2","最暖心的情侣软件");
map2.put("t1","小爱爱");
map2.put("t2","最有趣的情侣智能情趣用品");
map3.put("t1","美拍");
map3.put("t2","爱在没拍");
map4.put("t1","小恩爱");
map4.put("t2", "恋爱神器,只与心爱的人");

array.add(map);
array.add(map1);
array.add(map2);
array.add(map3);
array.add(map4);

Myadpter2 adpter1 = new Myadpter2(this,array);
list.setAdapter(adpter1);
}

}

class Myadpter2 extends BaseAdapter{

private ArrayList<HashMap<String,String>> mapArrayList;
private Context mContext;

public Myadpter2(Context mContext,ArrayList<HashMap<String,String>> mapArrayList){

this.mapArrayList = mapArrayList;
this.mContext = mContext;
}

@Override
public int getCount() {
return mapArrayList.size();
}

@Override
public Object getItem(int position) {
return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_apply_activith,null);

ImageView imagApple = (ImageView) convertView.findViewById(R.id.imagApply);
TextView txt1 = (TextView) convertView.findViewById(R.id.t1);
TextView txt2 = (TextView) convertView.findViewById(R.id.t2);
ImageButton image_Apply = (ImageButton) convertView.findViewById(R.id.imagButton);

Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.app_portrait);
imagApple.setImageBitmap(bitmap);
txt1.setText(mapArrayList.get(position).get("t1"));
txt2.setText(mapArrayList.get(position).get("t2"));


return convertView;
}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值