2

ViewFlow& CircleFlowIndicatorto view& showcircle を使用してindicate変更しますView

それでも私はshowing same textそれぞれのすべてのためにいますView、下の画像を参照してください:

ここに画像の説明を入力

ノート:

textView1に " " が、textView2 に " "Remember Lifeまだ表示されているInstantly search memories for friends ......

今、私は、に基づいてテキストを表示したい:View positionexample

Viewposition0の場合は textView1に " " を表示し、textView2 に " "TextView - 1.0表示する必要があり、Viewが1の場合はtextView1に " "を表示し、 textView2に" "を表示する必要がありますTextView - 2.0positionTextView - 1.1TextView - 2.1so on

しかし、毎回、私は-35になっNull Pointer Exceptionline numberます

つまり:position = viewFlow.getPositionForView(indic);

ViewFlow の現在のビューの位置を知るにはどうすればよいですか?

MainActivity.java:-

public class MainActivity extends Activity {

    private ViewFlow viewFlow;
    TextView textView1, textView2;
    int position = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.circle_title);
        setContentView(R.layout.screen_layout);

        viewFlow = (ViewFlow) findViewById(R.id.viewflow);
        viewFlow.setAdapter(new ImageAdapter(this), 1); // default focus on second circle

        CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);
        viewFlow.setFlowIndicator(indic);       

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

        position = viewFlow.getPositionForView(indic);
        Toast.makeText(MainActivity.this, position, Toast.LENGTH_LONG).show();

        switch (position) {
        case 0:
            textView1.setText("TextView - 1.0"); // TextView - 1, position - 0
            textView2.setText("TextView - 2.0"); // TextView - 2, position - 0
            break;

        case 1:
            textView1.setText("TextView - 1.1");
            textView2.setText("TextView - 2.1");
            break;

        case 2:
            textView1.setText("TextView - 1.2");
            textView2.setText("TextView - 2.2");
            break;

        case 3:
            textView1.setText("TextView - 1.3");
            textView2.setText("TextView - 2.3");
            break;

        case 4:
            textView1.setText("TextView - 1.4");
            textView2.setText("TextView - 2.4");
            break;

        default:

            break;
        }

    }

    /* If your min SDK version is < 8 you need to trigger the onConfigurationChanged in ViewFlow manually, like this */ 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        viewFlow.onConfigurationChanged(newConfig); 
    }
}

Logcatレポート:

08-22 06:54:53.811: W/dalvikvm(1829): threadid=1: thread exiting with uncaught exception (group=0x41465700)
08-22 06:54:53.840: E/AndroidRuntime(1829): FATAL EXCEPTION: main
08-22 06:54:53.840: E/AndroidRuntime(1829): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.indianic.viewflipperdemo/com.indianic.viewflipperdemo.CircleViewFlipperActivity}: java.lang.NullPointerException
08-22 06:54:53.840: E/AndroidRuntime(1829):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at android.os.Looper.loop(Looper.java:137)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at java.lang.reflect.Method.invokeNative(Native Method)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at java.lang.reflect.Method.invoke(Method.java:525)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at dalvik.system.NativeStart.main(Native Method)
08-22 06:54:53.840: E/AndroidRuntime(1829): Caused by: java.lang.NullPointerException
08-22 06:54:53.840: E/AndroidRuntime(1829):     at android.widget.AdapterView.getPositionForView(AdapterView.java:597)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at com.indianic.viewflipperdemo.CircleViewFlipperActivity.onCreate(CircleViewFlipperActivity.java:35)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at android.app.Activity.performCreate(Activity.java:5133)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-22 06:54:53.840: E/AndroidRuntime(1829):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-22 06:54:53.840: E/AndroidRuntime(1829):     ... 11 more

XML:-

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

     <com.indianic.viewflipperdemo.widget.ViewFlow
        android:id="@+id/viewflow"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:sidebuffer="3" >
    </com.indianic.viewflipperdemo.widget.ViewFlow>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:text="Remember Life"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/viewflowindic"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:padding="5dp"
        android:text="Instantly search memories of friends, season, birthdays, and more"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp" >

        <Button
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center"
            android:layout_weight="1"
            android:text="Register"
            android:textColor="#000000" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:text="Login"
            android:textColor="#000000" />
    </LinearLayout>

    <com.indianic.viewflipperdemo.widget.CircleFlowIndicator
        android:id="@+id/viewflowindic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:padding="20dp"
        app:inactiveType="stroke" />

</RelativeLayout>
4

2 に答える 2

2

例外の原因:

テキストを変更するために onCreate メソッドに switch-case を追加しました。これは正しくありません。ユーザー入力は、Java でイベント リスナーを実装することによって処理する必要があります。

解決:

ビューにリスナーを追加します。ビューがスワイプされると、リスナーはイベントを処理し、位置に従ってテキストを変更します。

ビューにsetOnViewSwitchListenerを追加し、 onSwitchedメソッドをオーバーライドします。

サンプルコード:

viewFlow.setOnViewSwitchListener(new ViewSwitchListener() {
    public void onSwitched(View v, int position) {
        //position is received as an argument from the system, it doesn't need to be calculated
        switch (position) {
        case 0:
            textView1.setText("TextView - 1.0"); // TextView - 1,
                                                    // position - 0
            textView2.setText("TextView - 2.0"); // TextView - 2,
                                                    // position - 0
            break;
            // ----rest of your code here------

参考文献:

于 2014-08-25T05:28:13.440 に答える
1

あなたの質問に対する解決策---

ここでは、ページの位置に応じてビューを見つけてカスタマイズできます。

MainActivity.Java

package com.example.circleindicatordemo;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;

import com.viewpagerindicator.CirclePageIndicator;

public class MainActivity extends FragmentActivity implements OnClickListener {
    MyAdapter mAdapter;
    ViewPager mPager;
    private CirclePageIndicator mIndicator;
    private static ArrayList<String> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        arrayList = new ArrayList<String>();
        arrayList.add("Page1");
        arrayList.add("Page2");
        arrayList.add("Page3");
        arrayList.add("Page4");
        arrayList.add("Page5");

        mAdapter = new MyAdapter(getSupportFragmentManager());
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
        mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
    }

    public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

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

        @Override
        public Fragment getItem(int position) {
            return MyFragment.init(position, arrayList);
        }
    }

    @Override
    public void onClick(View v) {

    }

}

MyFragment.Java--

package com.example.circleindicatordemo;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class MyFragment extends Fragment implements OnClickListener {
    int position;
    private ArrayList<String> arrayList;
    private TextView tv_name;
    private ImageView iv_image;

    static MyFragment init(int position, ArrayList<String> arrayList) {
        MyFragment giftFragment = new MyFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        args.putSerializable("data", arrayList);
        giftFragment.setArguments(args);
        return giftFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        position = getArguments().getInt("position");
        arrayList = (ArrayList<String>) getArguments().getSerializable("data");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_fragment, container, false);

        tv_name = (TextView) view.findViewById(R.id.tv_name);

        iv_image = (ImageView) view.findViewById(R.id.iv_image);
        tv_name.setText(arrayList.get(position));
        return view;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
}

上記のコードの完全なソースもここで確認できます

于 2014-08-25T07:05:17.387 に答える