按Gridviewpager时如何更改为另一个Activity?

时间:2015-02-09 20:48:43

标签: android onclicklistener android-gridview wear-os

我正在尝试根据我在Gridview中单击的页面启动一个新的某个Activity。 我试图了解示例GridViewPager,它与sdk一起出现并尝试调整given explanation。但是我真的不知道如何把这两件事放在一起,甚至从哪里开始。

第一个java.file选择

public class Selection extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selection_grid);
        final GridViewPager pager = (GridViewPager) findViewById(R.id.pager);

        pager.setAdapter(new Workers(this, getFragmentManager()));
        DotsPageIndicator dotsPageIndicator = (DotsPageIndicator) findViewById(R.id.page_indicator);
        dotsPageIndicator.setPager(pager);
    }
}

和第二个java.file用户:

public class Users extends FragmentGridPagerAdapter {

    private static final int TRANSITION_DURATION_MILLIS = 100;

    private final Context mContext;
    private List<Row> mRows;
    private ColorDrawable mDefaultBg;

    private ColorDrawable mClearBg;

    public Users (Context ctx, FragmentManager fm) {
        super(fm);
        mContext = ctx;

        mRows = new ArrayList<Workers.Row>();

        mRows.add(new Row(cardFragment(R.string.title, R.string.user1)));
        mRows.add(new Row(cardFragment(R.string.title, R.string.user2)));
        mRows.add(new Row(cardFragment(R.string.title, R.string.user3)));
        mRows.add(new Row(cardFragment(R.string.title, R.string.user4)));

//      In case in one row several cardFragments are needed
//        mRows.add(new Row(
//                cardFragment(R.string.cards_title, R.string.cards_text),
//                cardFragment(R.string.expansion_title, R.string.expansion_text)));

        mDefaultBg = new ColorDrawable(R.color.dark_grey);
        mClearBg = new ColorDrawable(android.R.color.transparent);
    }


    LruCache<Integer, Drawable> mRowBackgrounds = new LruCache<Integer, Drawable>(3) {
        @Override
        protected Drawable create(final Integer row) {
            int resid = BG_IMAGES[row % BG_IMAGES.length];
            new DrawableLoadingTask(mContext) {
                @Override
                protected void onPostExecute(Drawable result) {
                    TransitionDrawable background = new TransitionDrawable(new Drawable[] {
                            mDefaultBg,
                            result
                    });
                    mRowBackgrounds.put(row, background);
                    notifyRowBackgroundChanged(row);
                    background.startTransition(TRANSITION_DURATION_MILLIS);
                }
            }.execute(resid);
            return mDefaultBg;
        }
    };


   private Fragment cardFragment(int titleRes, int textRes) {
        Resources res = mContext.getResources();
        CardFragment fragment =
                CardFragment.create(res.getText(titleRes), res.getText(textRes));
        // Add some extra bottom margin to leave room for the page indicator
        fragment.setCardMarginBottom(
                res.getDimensionPixelSize(R.dimen.card_margin_bottom));
        return fragment;

-----------------Changed part -  Start (deleted the above part of the method)----------------
//        Resources res = mContext.getResources();
//        MyCardFragment.create(res.getText(titleRes), res.getText(textRes));
-----------------Changed part -  End------------------
        }


 static final int[] BG_IMAGES = new int[] {
            R.drawable.user1,
            R.drawable.user2,
            R.drawable.user3,
            R.drawable.user4
    };


  /** A convenient container for a row of fragments. */
    private class Row {
        final List<Fragment> columns = new ArrayList<Fragment>();

        public Row(Fragment... fragments) {
            for (Fragment f : fragments) {
                add(f);
            }
        }

        public void add(Fragment f) {
            columns.add(f);
        }

        Fragment getColumn(int i) {
            return columns.get(i);
        }

        public int getColumnCount() {
            return columns.size();
        }
    }


    @Override
    public Fragment getFragment(int row, int col) {
        Row adapterRow = mRows.get(row);
        return adapterRow.getColumn(col);
    }

    @Override
    public Drawable getBackgroundForRow(final int row) {
        return mRowBackgrounds.get(row);
    }


    @Override
    public int getRowCount() {
        return mRows.size();
    }

    @Override
    public int getColumnCount(int rowNum) {
        return mRows.get(rowNum).getColumnCount();
    }

    class DrawableLoadingTask extends AsyncTask<Integer, Void, Drawable> {
        private static final String TAG = "Loader";
        private Context context;

        DrawableLoadingTask(Context context) {
            this.context = context;
        }

        @Override
        protected Drawable doInBackground(Integer... params) {
            Log.d(TAG, "Loading asset 0x" + Integer.toHexString(params[0]));
            return context.getResources().getDrawable(params[0]);
        }
    }
}

编辑: 第三个java.file MyCardFragment

public class MyCardFragment extends CardFragment {

private View fragmentView;

@Override
protected View onCreateContentView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
    fragmentView = super.onCreateContentView(inflater, container, savedInstanceState);
    return fragmentView;
}

public void setOnClickListener(final View.OnClickListener listener) {
    this.fragmentView.setOnClickListener(listener);
    }

}

更新LOGCAT:

02-24 13:53:18.149    3104-3104/de.denjo.fragmentapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: de.denjo.fragmentapplication, PID: 3104
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.denjo.fragmentapplication/android.text.Selection}; have you declared this activity in your AndroidManifest.xml?
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
            at android.app.Activity.startActivityForResult(Activity.java:3736)
            at android.app.Activity.startActivityForResult(Activity.java:3697)
            at android.app.Activity.startActivity(Activity.java:4007)
            at android.app.Activity.startActivity(Activity.java:3975)
            at de.denjo.fragmentapplication.MainActivity.OnClickSelection(MainActivity.java:35)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:1)

我将如何做到这一点

扩展CardFragment:

class MyCardFragment extends CardFragment {

     private View fragmentView;     
     private OnClickListener listener;

     @Override
     protected View onCreateContentView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
         fragmentView = super.onCreateContentView(inflater, container, savedInstanceState);
         fragmentView.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(final View view) {
                 if (listener != null) {
                     listener.onClick(view);
                 }
             }    

         }
         return fragmentView;
     }

     public void setOnClickListener(final OnClickListener listener) {
         this.listener = listener;
     }

}

这就是全部:)

UPD(还要查看更新的MyCardFragment,否则你会得到NullPointerException!):

private Fragment cardFragment(int titleRes, int textRes) {
        Resources res = mContext.getResources();
        final MyCardFragment fragment = MyCardFragment.create(res.getText(titleRes), res.getText(textRes));
        fragment.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(final View view) {
                    //do everything you want
                    //here you've got access to clicked fragment
             }

        }
        return fragment;
}