Wechsel zu einer neuen Activity im Gridviewpager (Android Wear)

  • Antworten:1
  • OffenNicht stickyNicht beantwortet
Gelöschter Account
  • Forum-Beiträge: 3

09.02.2015, 10:27:57 via Website

Hallo,

ich versuche in einem Gridviewpager auf der Wear eine entsprechende Activity zu öffnen, je nach dem auf welches Feld/Fenster ich drücke. Ich hab versucht das Android Wear SDK Sample Gridviewpager zu verstehen und die einzig auf stackoverflow (Question # 26343337) zu adaptieren. Ich steige überhaupt nicht dahinter, wie ich die beiden zusammenbringen kann. Falls weitere Informationen benötigt werden, bitte einfach fragen.

Hier mein Code:

Selection.java

   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);
    }
}

Users.java

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;
    }


 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]);
        }
    }
}

— geändert am 11.02.2015, 10:40:49

Antworten
Gelöschter Account
  • Forum-Beiträge: 3

11.02.2015, 10:41:39 via Website

Bin leider immer noch nicht weiter. Bitte deshalb weiterhin um Hilfe.

Antworten

Empfohlene Artikel