android - 请教,安卓listview点击事件
高洛峰
高洛峰 2017-04-17 15:36:13
0
2
626

这个程序是有点小题大做。问题是这样的,我程序中没有给listView加上Onclicklistener监听啊,为什么运行后,我随意点击任何一个ListView的items时候都弹出footer呢?

看程序吧:

主程序:

package com.example.NotifyDataSetChangeDemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

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

public class MyActivity extends Activity implements View.OnClickListener{

        private List<Article> list;
        private ListView listView;
        private MyAdapter adapter;
        private Button loadBtn, loadMoreBtn,loadingBtn;
        private Handler handler;
        private LayoutInflater mInflater;
        private View footer;


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

                initView();
                loadBtn.setOnClickListener(this);
                loadMoreBtn.setOnClickListener(this);
        }

        private void initView() {

                loadBtn = (Button) findViewById(R.id.load);
                loadMoreBtn = (Button) findViewById(R.id.loadMore);
                loadingBtn = (Button) findViewById(R.id.loading);
                list = new ArrayList<Article>();
                listView = (ListView) findViewById(R.id.listView);
                adapter = new MyAdapter(list, MyActivity.this);
                handler = new Handler();
                mInflater = LayoutInflater.from(MyActivity.this);
                footer = mInflater.inflate(R.layout.footer, null);
                footer.setVisibility(View.GONE);
                listView.addFooterView(footer);

        }


        private void CreateDatas() {
                if (list.size()==0){
                        for (int i = 0; i < 10; i++) {
                                Article article = new Article();
                                article.setTitle("I am a dog" + i);
                                article.setContent("Yes,you are a big dog," + i);
                                list.add(article);
                        }
                }else {

                        Toast.makeText(MyActivity.this,"You had loaded the original datas,please stop!",Toast.LENGTH_SHORT).show();
                }


        }

        private void CreateMoreDatas() {
                int nowSize = list.size();
                for (int i = nowSize; i < nowSize+5; i++) {
                        Article article = new Article();
                        article.setTitle("I am new" + i);
                        article.setContent("you are old man,guy," + i);
                        list.add(article);
                }

        }



        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.load:
                                CreateDatas();
                                listView.setAdapter(adapter);
                                break;
                        case R.id.loadMore:
                                footer.setVisibility(View.VISIBLE);
                                handler.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {

                                                CreateMoreDatas();
                                                adapter.notifyDataSetChanged();
                                                listView.setAdapter(adapter);
                                                footer.setVisibility(View.GONE);
                                        }
                                }, 3000);

                }
        }
}

适配器:

package com.example.NotifyDataSetChangeDemo;

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

import java.util.List;


public class MyAdapter extends BaseAdapter {

        private List<Article> list;
        private Context context;
        private LayoutInflater mInflater;

        public MyAdapter(List<Article> list, Context context) {
                this.list = list;
                this.context = context;
                mInflater = LayoutInflater.from(context);
        }

        @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;
                if (convertView==null){
                        convertView = mInflater.inflate(R.layout.items, null);
                        holder = new ViewHolder(convertView);
                        holder.title = (TextView) convertView.findViewById(R.id.title);
                        holder.content = (TextView) convertView.findViewById(R.id.content);
                        convertView.setTag(holder);
                }else{

                        holder = (ViewHolder) convertView.getTag();
                }

                holder.title.setText(list.get(position).getTitle());
                holder.content.setText(list.get(position).getContent());

                return convertView;
        }


}

class ViewHolder{

        View view;
         TextView title;
         TextView content;
        public ViewHolder(View view) {
                this.view = view;
        }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <Button
                android:id="@+id/load"
                android:text="加载数据"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        <Button
                android:id="@+id/loadMore"
                android:text="加载更多"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    </LinearLayout>

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

footer.xml

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

    <Button
            android:id="@+id/loading"
            android:text="now loading...."
            android:textSize="30dp"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>

items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
            android:id="@+id/title"
            android:textSize="25dp"
            android:textStyle="bold"
            android:maxLines="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <TextView
            android:id="@+id/content"
            android:textSize="18dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

모든 응답(2)
Peter_Zhu

시스템 문제, 예전에는 CM 시스템을 사용했는데 정품으로 교체하면 괜찮을 것 같습니다. 정말 실수입니다

迷茫

당신이 게시한 코드가 당신이 언급한 상황을 일으키지 않는다는 것을 주의 깊게 볼 필요는 없다고 생각합니다. 코드를 변경했다면 문제가 있는 코드를 게시하고 사실에 근거해 솔직하게 논의하세요.

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿