Android实现listview布局,点击activity传递跳转,长按跳出对话框,退出提示Dialog对话框

xml进行布局<----item.XML---->

<?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="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:padding="5dp"
        android:gravity="center_vertical"
        android:descendantFocusability="blocksDescendants">  //抢夺焦点
        <ImageView
            android:id="@+id/logo"
            android:layout_width="70dip"
            android:layout_height="70dip"
            android:src="@drawable/timg1"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginLeft="5dp"
            android:layout_weight="1">
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="知乎网"
                android:layout_marginTop="10dip"/>
            <TextView
                android:id="@+id/vertion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="版本:1.0.1"
                android:textSize="10sp"
                android:textColor="#999"
                android:layout_marginTop="6dp"/>
            <TextView
                android:id="@+id/size"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="大小:45M"
                android:textSize="10sp"
                android:textColor="#999"
                android:layout_marginTop="3dp"/>
        </LinearLayout>
        <Button
            android:layout_width="75dip"
            android:layout_height="35dip"
            android:background="@drawable/btn_selector"    //接入按钮图片颜色
            android:text="下载"/>
    </LinearLayout>
</LinearLayout>

listview进行布局  <----mainactivity.java----->

package com.yongninggo.helloworld3;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    ListView lv_show ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView lv_show = (ListView) findViewById(R.id.lv_show);
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("logo",R.drawable.timg1);
        map.put("title","知乎网");
        map.put("vertion","版本:1.0.1");
        map.put("size","大小:45M");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("logo",R.drawable.timg2);
        map.put("title","微信");
        map.put("vertion","版本:1.0.1");
        map.put("size","大小:45M");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("logo",R.drawable.timg3);
        map.put("title","QQ");
        map.put("vertion","版本:1.0.1");
        map.put("size","大小:45M");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("logo",R.drawable.timg4);
        map.put("title","微博");
        map.put("vertion","版本:1.0.1");
        map.put("size","大小:45M");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("logo",R.drawable.timg5);
        map.put("title","支付宝");
        map.put("vertion","版本:1.0.1");
        map.put("size","大小:45M");
        list.add(map);

{将simpleadapter适配器修改为自定义Myadapter适配器}
        /*SimpleAdapter adapter = new SimpleAdapter(
                this,
                list,
                R.layout.item,
                new String[]{"logo","title","vertion","size"},
                new int[]{R.id.logo,R.id.title,R.id.vertion,R.id.size}
                );*/

        Myadapter adapter = new Myadapter(this);
        adapter.setList(list);

        lv_show.setAdapter(adapter);

        lv_show.setOnItemClickListener(this);
        lv_show.setOnItemLongClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Toast.makeText(this,"点击"+position,Toast.LENGTH_SHORT).show();
Activity数据传递
        Intent intent = new Intent();
        intent.setClass(this,aActivity.class);
        intent.putExtra("index",""+position);
        Map<String,Object> map = (Map<String, Object>) parent.getItemAtPosition(position);
        intent.putExtra("title",""+map.get("title"));
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        //Toast.makeText(this,"长按"+position,Toast.LENGTH_SHORT).show();
长按listview触发事件
        final String [] arr = {"复制","删除","举报"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.timg1);
        builder.setTitle("操作");
        builder.setItems(arr, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"您选择了:"+arr[which],Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();

        return true;
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
退出提示事件
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.timg2);
        builder.setTitle("操作");
        builder.setMessage("您确定退出吗?");
        builder.setNegativeButton("取消",null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.show();
    }
}

Activity事件<----activity.java---->

package com.yongninggo.helloworld3;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

/**
 * Created by 14487 on 2017/5/6.
 */

public class aActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);

        String index = getIntent().getStringExtra("index");
        String title = getIntent().getStringExtra("title");
        TextView info = (TextView) findViewById(R.id.info);
        info.setText("序列号:"+index+"   软件名:"+title);
    }
}

自定义适配器内容  <----Myactivity.java---->


package com.yongninggo.helloworld3;

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

import java.util.List;
import java.util.Map;

/**
 * Created by 14487 on 2017/5/6.
 */

public class Myadapter extends BaseAdapter {
    List<Map<String,Object>> list ;
    LayoutInflater inflater ;

    public Myadapter(Context context) {
        this.inflater = LayoutInflater.from(context);
    }

    public void setList(List<Map<String, Object>> list) {
        this.list = list;
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder holder = null;  //二级优化定义
        if (convertView == null){  //listview一级优化
            convertView = inflater.inflate(R.layout.item,null);
            holder = new viewHolder();
            holder.logo = (ImageView) convertView.findViewById(R.id.logo);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.vertion = (TextView) convertView.findViewById(vertion);
            holder.size = (TextView) convertView.findViewById(size);
            convertView.setTag(holder);
        } else {
            holder = (viewHolder) convertView.getTag();
        }

        Map map = list.get(position);
        holder.logo.setImageResource((Integer) map.get("logo"));
        holder.title.setText((String) map.get("title"));
        holder.vertion.setText((String) map.get("vertion"));
        holder.size.setText((String) map.get("size"));

        return convertView;
    }
    public class viewHolder{
        ImageView logo;
        TextView title;
        TextView vertion;
        TextView size;
    }
}

示例如图:

Android实现listview布局,点击activity传递跳转,长按跳出对话框,退出提示对话框 - Android - Android的博客            Android实现listview布局,点击activity传递跳转,长按跳出对话框,退出提示对话框 - Android - Android的博客


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值