Android listactivity 滚动与加速度计问题

标签 android listview scroll accelerometer

我的 Android 应用程序中有一个运行良好的 ListActivity。 直到我决定使用加速度计添加滚动功能。

这是我的代码:

package be.pxl.minecraftguide;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;
import be.pxl.minecraftguide.providers.RecipeCategoryProvider;

public class Crafting extends ListActivity implements SensorEventListener {
    private SimpleCursorAdapter adapter;
    private SensorManager sensorManager;
    private Sensor acceleroMeter;
    private float[] history = new float[2];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.categorylist);

        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(RecipeCategoryProvider.CONTENT_URI, null, null, null, null);
        String[] from = {RecipeCategoryProvider.COL_CATID, RecipeCategoryProvider.COL_CATIMG, RecipeCategoryProvider.COL_CATDESC};
        int[] to = { R.id.txtCatID, R.id.imgCatImage, R.id.txtCatDescription };
        adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.categoryrow, cursor, from, to, 0);
        /*SimpleCursorAdapter.ViewBinder viewBinder = new SimpleCursorAdapter.ViewBinder() {

            @Override
            public boolean setViewValue(View view, Cursor cursor, int arg2) {
                if(view.getId() == R.id.imgCatImage) {
                    ImageView image = (ImageView)findViewById(R.id.imgCatImage);
                    image.setImageResource(R.drawable.ic_launcher);
                }

                return false;
            }

        };*/

        setListAdapter(adapter);

        //__________BRON/ http://stackoverflow.com/questions/18751878/android-using-the-accelerometer-to-create-a-simple-maraca-app_____________

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        acceleroMeter = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, acceleroMeter, 1000000);

        /*recipeListView.setOnClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View row,
                    int position, long id) {

                    TextView txtId = (TextView)row.findViewById(R.id.txtRecipeID);
                    int recipeID = Integer.parseInt(txtId.getText().toString());
                    String description = ((TextView)row.findViewById(R.id.txtRecipeDescription)).getText().toString();


                    // Launching new Activity on selecting single List Item
                    // Intent craftingDetailIntent = new Intent(getApplicationContext(), craftingdetail.class);
                    // sending data to new activity
                    //craftingDetailIntent.putExtra("Category", item);
                    //startActivity(craftingDetailIntent); 
                }
        });*/
    }

    /*@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

        int recipeID = Integer.parseInt(v.getTag().toString());
    }*/

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        sensorManager.unregisterListener(this); //De accelerometer afzetten
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        sensorManager.registerListener(this, acceleroMeter, SensorManager.SENSOR_DELAY_UI); //De accelerometer opnieuw starten
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float xChange = history[0] - event.values[0];
            float yChange = history[1] - event.values[1]; //Verschil tussen nieuwe en oude positie ophalen.

            history[0] = event.values[0];
            history[1] = event.values[1]; //Nieuwe waarden bewaren voor volgende event trigger

            if (xChange > 2){
                //Links
            }
            else if (xChange < -2){
                //Rechts
            }

            if (yChange > 2){
                    getListView().smoothScrollBy(getListView().getHeight() * adapter.getCount(), 2000);
                    getListView().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            getListView().smoothScrollBy(0, 0); //Geanimeerd scrollen naar laatste positie
                            getListView().setSelection(adapter.getCount() - 1);
                        }
                    }, 1000);
            }
            else if (yChange < -2){
                getListView().smoothScrollBy(getListView().getHeight() * adapter.getCount(), 2000);
                getListView().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getListView().smoothScrollBy(0, 0); //Geanimeerd scrollen naar eerste positie positie
                        getListView().setSelection(0);
                    }
                }, 1000);
            }
        }

    }


}

问题是,当我向上移动时,列表应该向下滚动并停留在底部。但是我的列表会向下滚动,然后偶尔会向上滚动。向下运动也是如此。

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/minecraft_portrait"
    android:transcriptMode="alwaysScroll" >


</ListView>

并不太相关,但这是我的行 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recipeRow"
    android:layout_width="match_parent"
    android:layout_height="65sp"
    android:background="@drawable/listview_selector" >

    <TextView
        android:id="@+id/txtCatID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:textColor="@android:color/black" />

    <ImageView
        android:id="@+id/imgCatImage"
        android:layout_width="wrap_content"
        android:layout_height="55sp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher"
        android:contentDescription="@string/recipecategory" />

    <TextView
        android:id="@+id/txtCatDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/imgCatImage"
        android:gravity="center"
        android:background="#99FFFFFF"
        android:textSize="20sp"
        android:textColor="@android:color/black"
        android:text="test" />

</RelativeLayout>

我该如何解决这个问题?

最佳答案

我一直在寻找使用传感器滚动 ListActivity 的相同解决方案(在 Google Glass 的上下文中),而另一个答案对我不起作用(滚动不稳定)。

不过,我能够让另一个版本正常工作,仅基于 ListView(mList,在下面的示例中)。请注意,这还假设一次屏幕上的项目数量是固定的(因为所有 Glass 显示器的尺寸都相同),但是可以更改此设置以检查屏幕上有多少项目以获得更通用的 Android 解决方案。此解决方案还使用 Sensor.TYPE_ROTATION_VECTOR ,它在支持的设备上进行一些传感器融合,因此输出噪音较小(导致不需要的上下移动较少)。

注册监听器:

mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(this,
        mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),
        SensorManager.SENSOR_DELAY_UI);

...这是 onSensorChanged() 方法:

@Override
public void onSensorChanged(SensorEvent event) {
    if (mList == null) {
        return;
    }

    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
        SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);

        // Only uncomment the below two lines if you're running on Google Glass
        //SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X,
        //    SensorManager.AXIS_Z, mRotationMatrix);
        SensorManager.getOrientation(mRotationMatrix, mOrientation);

        mHeading = (float) Math.toDegrees(mOrientation[0]);
        mPitch = (float) Math.toDegrees(mOrientation[1]);

        float xDelta = history[0] - mHeading;  // Currently unused
        float yDelta = history[1] - mPitch;

        history[0] = mHeading;
        history[1] = mPitch;

        float Y_DELTA_THRESHOLD = 0.10f;

        //Log.d(TAG, "Y Delta = " + yDelta);

        int scrollHeight = mList.getHeight()
            / 19; // 4 items per page, scroll almost 1/5 an item

        //Log.d(TAG, "ScrollHeight = " + scrollHeight);

        if (yDelta > Y_DELTA_THRESHOLD) {
            //Log.d(TAG, "Detected change in pitch up...");
            mList.smoothScrollBy(-scrollHeight, 0);
        } else if (yDelta < -Y_DELTA_THRESHOLD) {
            //Log.d(TAG, "Detected change in pitch down...");
            mList.smoothScrollBy(scrollHeight, 0);
        }
    }
}

其他详细信息在这个答案中: https://stackoverflow.com/a/23298377/937715

有关重新映射传感器坐标系以处理 Android 设备上所有可能的方向变化的代码,请参阅: https://stackoverflow.com/a/22138449/937715

关于Android listactivity 滚动与加速度计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19540828/

相关文章:

android - 获取平铺位图/重复位图

java - ListView 与 ArrayAdapter 中的 setOnItemClickListener 错误

ios - (XCODE) TextView : shows bottom of text as first

javascript - 滚动一次动画

java - 如何在 JList 上添加滚动条? [JAVA]

android - 从对话框更新 RecyclerView

Android - 加快向数据库中插入数据的速度

android - 如果同时使用 iOS,Google Play Services 8.4.0 会破坏适用于 Android 的 Google Cloud Messaging

Android删除自定义数组适配器中的 ListView 行

android - 三星 Touchwiz 列出小部件