7

私はこのCoverFlowを使用しています:http: //www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html

ボタンをクリックしたときにビューを変更できるようにしたいのですが、ボタンはカバーフローの前/次のアイテムに移動する戻る/進むアイコンです。

代わりにXMLレイアウトを使用するように、カバーフローを少し変更しました。

これが私のonCreate()メソッドです:

 setContentView(R.layout.main);

 CoverFlow coverFlow = (CoverFlow)findViewById(R.id.coverflow);  

 coverFlow.setAdapter(new ImageAdapter(this));
 ImageAdapter coverImageAdapter =  new ImageAdapter(this);     
 coverFlow.setAdapter(coverImageAdapter);
 coverFlow.setSpacing(-20);
 coverFlow.setSelection(0, true);
 coverFlow.setAnimationDuration(1000);

 tv = (TextView)findViewById(R.id.textView1);

 coverFlow.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Toast.makeText(getBaseContext(), String.valueOf(arg2), Toast.LENGTH_SHORT).show();
    }
 });

 coverFlow.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        tv.setText(String.valueOf(arg2));
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // Do something
    }
 });

私のXML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/home_background"
    android:orientation="vertical" >

    <com.demo.app.coverflow.CoverFlow
        android:id="@+id/coverflow"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="39dp"
        android:layout_marginLeft="35dp"
        android:background="@drawable/button_left" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="39dp"
        android:layout_marginRight="35dp"
        android:background="@drawable/button_right" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="55dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="hello"
        android:textColor="@color/White"
        android:textSize="16dp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="170dp"
        android:layout_marginTop="15dp"
        android:src="@drawable/unsa_logo" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="14dp"
        android:layout_marginTop="23dp"
        android:src="@drawable/pnc_logo" />

</RelativeLayout>
4

3 に答える 3

3

アニメーションを追加する方法を見つけようとしていたときに、すべてを1行で実行するアニメーションに出くわしました。

左に行く

coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, new KeyEvent(0, 0));

右へ曲がって

coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(0, 0));

(クリックメソッドのそれぞれのボタンに各行を追加するだけです

これが私と同じ状況で自分自身を見つける他の人を助けることができることを願っています:)

于 2012-11-26T11:03:08.457 に答える
3

Davidに感謝します。私はMikeの同僚の一人ですが、Javaの行を書いたことがありません。私は当社のiOS部門で働いています。私はあなたが話していることを理解しました。あなたの答えは役に立ち、よく書かれていました!

追加する必要がありました:

if ( currentimageposition < coverFlow.getcount()-1) {
     coverFlow.setSelection(currentImagePosition +1, true);
     currentImagePosition = currentImagePosition +1;
}

onItemSelectedが呼び出されなかったため(現在のマーカーを更新)、理由はわかりません。

ただし、coverFlow.selection( int , bool)アニメーション化されていません。これは、ビューがロードされるときの単なる設定メソッドです。たとえばcoverFlow.selection(3,true)、4番目の画像をCoverflowの中央に設定します。

于 2012-11-20T17:26:11.310 に答える
2

私はあなたの質問を理解することを望みます。もしそうなら、ここにアイデアがあります:

カバーフローから表示されている画像の現在の位置を追跡するプライベートインスタンス変数を保存する必要があります。setOnItemSelectedListener()次に、 Galleryから継承されたメソッドから更新します。「戻る」または「進む」ボタンでは、押されたボタンに応じて、現在の選択を+/-1に設定するだけです。

したがって、アクティビティに、次のような位置を追跡するインスタンス変数intを追加します...

public class MyActivity {
    private int currentImagePosition = -1;

    //add the rest of your onCreate code here...
}

次に、Coverflowに現在表示されている画像の現在の位置を更新できるようにする必要があります。setOnItemSelectedListener(onCreateメソッド内で)そのようにオーバーライドすることでこれを行うことができます:

coverFlow.setOnItemSelectedListener(new OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentImagePosition = position; //this will update your current marker
            }
};

setSelection( currentImagePosition+1 )最後に、またはを使用して前または次の画像に変更するように各ボタンのonclickリスナーを設定するだけですsetSelection( currentImagePosition-1 )。ちなみに、doの真または偽のパラメータはsetSelection()何ですか?何をするのかわからないので、true最初と同じように使います。onCreate()メソッドは次のようになります。

@Override
public void onCreate(Bundle savedInstanceState){
    //insert your other code in onCreate here in this spot.... 

    //add the following lines to your code:
    Button goLeft = (Button) findViewById(R.id.button1);
    goLeft.setOnClickListener( new OnClickListener() {
        @Override
        public void onClick(View v) {
                //go to previous image
                        if( currentImagePosition > 0 ){ //i'm assuming you dont want to go left anymore once at the leftmost image
                             coverFlow.setSelection(currentImagePosition - 1, true);
                        }
        }
    } ) ;


    Button goRight = (Button) findViewById(R.id.button2);
    goRight.setOnClickListener( new OnClickListener() {
        @Override
        public void onClick(View v) {
                //go to previous image
                        if( currentImagePosition < coverFlow.getCount()-1 ){ //i'm assuming you dont want to go left anymore once at the leftmost image
                             coverFlow.setSelection(currentImagePosition + 1, true);
                        }
        }
    } ) ;


}

注:位置の更新に関する部分は、 「ギャラリーに表示されている現在の画像の位置を取得する」を読んで機能すると想定されている ため、これが機能することを願っています。そうでない場合は、少なくとも私は試しました:D

幸運を!!!

于 2012-11-09T22:46:49.033 に答える