Android ListFragment更新/刷新和onItemClick

时间:2015-03-08 02:04:29

标签: android-fragments onclick android-listfragment notifydatasetchanged onitemclick

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

当我点击该项目时,我也希望将一个轨道静音,但每次都不会触发onItemClick事件。只是有时。任何人都可以向我解释这个吗?我在这做错了什么? 它在没有notifyDataSetChanged()的情况下工作得很好,但随后项目不会刷新。

谢谢!

这是完整的代码:

Mainactivity:

package de.sma.stackoverflow;

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.stackoverflow;

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.stackoverflow;

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.stackoverflow;

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.stackoverflow.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>

1 个答案:

答案 0 :(得分:1)

你的代码中的一切都很好。没有被点击的项目的主要原因是因为你使用了while (true)语句总是如此。将不会单击当前正在线程内处理的项目,并且当前从线程中排除的项目是可单击的。但是你必须让你的线程不要在循环内运行。因此,您可以执行的操作是删除while(true)语句,或将其替换为if(true)