Android ListFragment update/refresh 和 onItemClick

Android ListFragment update/refresh and onItemClick

我的 Android 项目有问题。它是一个音乐曲目播放器,在 ListFragment 中显示每个曲目。每个项目都有一个位图,位图上绘制了一个示波器 canvas。 Listfragment 中的项目会定期刷新。这是通过 notifyDataSetChanged();

我还想在单击该项目时将一首曲目静音,但每次都不会触发 onItemClick 事件。只有某些时候。谁能给我解释一下?我在这里做错了什么? 它在没有 notifyDataSetChanged() 的情况下工作完美,但项目不会刷新。

谢谢!

这是完整代码:

主要活动:

package de.sma.Whosebug;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

MyListFragment

package de.sma.Whosebug;

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

import android.app.ListFragment;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class MyListFragment extends ListFragment implements OnItemClickListener
{
    ListViewAdapter adapter;
    private List<ListViewItem> rowItems;
    private Handler sampleUpdateHandler = new Handler();
    Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
    boolean bMuted = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        return inflater.inflate(R.layout.fragment_list_layout, null, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        paintText.setColor(Color.WHITE);
        paintText.setStyle(Paint.Style.FILL);

        rowItems = new ArrayList<ListViewItem>();
        rowItems.add(new ListViewItem("Android"));
        rowItems.add(new ListViewItem("iOS"));
        rowItems.add(new ListViewItem("Symbian"));
        rowItems.add(new ListViewItem("Blackberry"));
        rowItems.add(new ListViewItem("Windows Phone"));
        rowItems.add(new ListViewItem("Galaxy Nexus"));
        rowItems.add(new ListViewItem("Offline"));

        adapter = new ListViewAdapter(getActivity(), rowItems);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
        StartThread();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Log.d("WhyDoesntThisWork", "onListItemClick");
        bMuted = !bMuted;
    }


    private void StartThread()
    {
        new Thread(new Runnable()
        {
            Random rand = new Random();

            public int randInt(int min, int max)
            {
                int randomNum = rand.nextInt((max - min) + 1) + min;
                return randomNum;
            }

            @Override
            public void run()
            {
                while (true)
                {
                    try
                    {
                        Thread.sleep(10);
                        sampleUpdateHandler.post(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                Canvas myCanvas = rowItems.get(0).getCanvas();
                                if (bMuted)
                                    myCanvas.drawColor(0xFFFF0000);
                                else
                                    myCanvas.drawColor(0xFF000020);
                                for (int i = 0; i < myCanvas.getWidth(); i++)
                                    myCanvas.drawLine(i, randInt(0, 50), i + 1, randInt(0, 50), paintText);

                                adapter.notifyDataSetChanged();
                            }
                        });
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

ListViewItem:

package de.sma.Whosebug;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.widget.ImageView;

public class ListViewItem
{

    private String title;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private ImageView mImageView;

    public ListViewItem(String title)
    {
        this.title = title;
        mBitmap = Bitmap.createBitmap(100, 50, Bitmap.Config.RGB_565);
        mCanvas = new Canvas(mBitmap);
        mCanvas.drawColor(0xFF2020FF);
    }

    public Canvas getCanvas()
    {
        return mCanvas;
    }

    public String getTitle()
    {
        return title;
    }

    public Bitmap getBitmap()
    {
        return mBitmap;
    }

    public void setImageView(ImageView imageView)
    {
        this.mImageView = imageView;
    }
}

ListViewAdapter:

package de.sma.Whosebug;

import android.app.Activity;
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;

public class ListViewAdapter extends BaseAdapter
{
    Context context;
    List<ListViewItem> rowItem;

    ListViewAdapter(Context context, List<ListViewItem> rowItem)
    {
        this.context = context;
        this.rowItem = rowItem;
    }

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

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

    @Override
    public long getItemId(int position)
    {
        return rowItem.indexOf(getItem(position));
    }

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

        if (convertView == null)
        {
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_item, null);
        }

        TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

        ListViewItem row_pos = rowItem.get(position);
        // setting the image resource and title
        txtTitle.setText(row_pos.getTitle());

        ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
        imageView.setImageBitmap(row_pos.getBitmap());
        row_pos.setImageView(imageView);

        return convertView;
    }
}

主要布局:

<?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" >

    <fragment
        android:id="@+id/fragment1"
        android:name="de.sma.Whosebug.MyListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</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="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TextView>
</LinearLayout>

列表项:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:contentDescription="imgdesc"
        android:layout_alignParentTop="true"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:textColor="#000000"
        android:textSize="20sp"
        android:text="test"/>
</RelativeLayout>

您的代码中的一切都很好,没有被点击的主要原因是因为您使用了 while (true) 始终正确的语句。当前正在线程内处理的项目将不会被点击,而当前被排除在线程之外的项目是可点击的。你必须如何让你的线程不进入 运行 内部循环。因此,您可以做的是删除 while(true) 语句,或者将其替换为 if(true).