1

他们无论如何都是为亚马逊消防电视实施轮播的方式。当我实现轮播时,它在手机上运行良好,但是当我尝试为消防电视创建它时。他们是否有任何其他方式来实现轮播并访问轮播中每个图像的位置并突出显示它//这里是 Java 代码

        import android.support.v7.app.ActionBarActivity;
        import android.content.Context;
        import android.graphics.Color;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.KeyEvent;
        import android.view.LayoutInflater;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.View.OnKeyListener;
        import android.view.ViewGroup;
        import android.widget.AdapterView;
        import android.widget.AdapterView.OnItemClickListener;
        import android.widget.BaseAdapter;
        import android.widget.ImageView;
        import android.widget.Toast;

        import com.digitalaria.gama.carousel.Carousel;
        public class MainActivity extends ActionBarActivity {
            private Carousel carousel;
            private ImageAdapter adapter;

        private int[] musicCover = { R.drawable.pic_1, R.drawable.pic_15,
                    R.drawable.pic_2, R.drawable.pic_8, R.drawable.pic_9,
                    R.drawable.pic_1,R.drawable.pic_15,R.drawable.pic_1, R.drawable.pic_15,
                    R.drawable.pic_2, R.drawable.pic_8, R.drawable.pic_9,
                    R.drawable.pic_1,R.drawable.pic_15
                    };

            @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            init();
        }
        private void init()
        {
             // create the carousel object.
            carousel = (Carousel) findViewById(R.id.carousel);
        //  Toast.makeText(getApplicationContext(),carousel.getAngle(),Toast.LENGTH_LONG).show();   
            carousel.setType(Carousel.TYPE_CYLINDER);
            carousel.setOverScrollBounceEnabled(true);
            carousel.setInfiniteScrollEnabled(true);
            carousel.setItemRearrangeEnabled(true);
            carousel.setOnItemClickListener(new OnItemClickListener() {

                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    switch (keyCode){
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                        Log.d("test", "test");
                    case KeyEvent.KEYCODE_BUTTON_A:
                // ... handle selections
                        Log.d("test", "test");
                        //handled = true;
                        break;
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                // ... handle left action
                        Log.d("test", "test");
                        //handled = true;
                        break;
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                // ... handle right action
                        Log.d("test", "test");
                        //handled = true;
                        break;
                    }
                    return false;
                }

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {
                    // TODO Auto-generated method stub
                    carousel.setOnItemClickListener(new OnItemClickListener() {

                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            // TODO Auto-generated method stub
                            switch (keyCode){
                            case KeyEvent.KEYCODE_DPAD_CENTER:
                                Log.d("test", "test");
                            case KeyEvent.KEYCODE_BUTTON_A:
                        // ... handle selections
                                Log.d("test", "test");
                                //handled = true;
                                break;
                            case KeyEvent.KEYCODE_DPAD_LEFT:
                        // ... handle left action
                                Log.d("test", "test");
                                //handled = true;
                                break;
                            case KeyEvent.KEYCODE_DPAD_RIGHT:
                        // ... handle right action
                                Log.d("test", "test");
                                //handled = true;
                                break;
                            }
                            return false;
                        }

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
                            // TODO Auto-generated method stub
                            Log.d("OnItem ","click"+"position="+position+" id="+id);

                        }
                    });
                    Log.d("OnItem ","click"+"position="+position+" id="+id);

                }
            });
            carousel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(),carousel.getAngle(),Toast.LENGTH_LONG).show();   
                }
            });

            // set images for the carousel.
            adapter = new ImageAdapter(this);
            carousel.setAdapter(adapter);

            // change the first selected position.
            //carousel.setCenterPosition(3);
        }
        public class ImageAdapter extends BaseAdapter {
            private Context mContext;

            public ImageAdapter(Context c)
            {
                mContext = c;
            }

            @Override
            public int getCount() {
                  return musicCover.length;
            }

            @Override`enter code here`
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return musicCover[position];
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = convertView;
                if (view == null) {
                    view = LayoutInflater.from(mContext).inflate(R.layout.carousel_item, parent, false);                
                    view.setLayoutParams(new Carousel.LayoutParams(250, 250));

                    ViewHolder holder = new ViewHolder();
                    holder.imageView = (ImageView)view.findViewById(R.id.itemImage);

                    view.setTag(holder);
                    holder.imageView.setFocusable(true);

                }

                ViewHolder holder = (ViewHolder)view.getTag();
                holder.imageView.setImageResource(musicCover[position]);
                holder.imageView.setOnKeyListener(new OnKeyListener() {

                    public boolean onKey(View v, int keycode, KeyEvent keyevent) {
                        // TODO Auto-generated method stub

                        boolean handled = false;

                        switch (keycode){
                            case KeyEvent.KEYCODE_DPAD_UP:
                                Log.d("test UP", "test UP");
                            case KeyEvent.KEYCODE_DPAD_DOWN:
                        // ... handle selections
                                Log.d("test down", "test Down");
                                handled = true;
                                break;
                            case KeyEvent.KEYCODE_DPAD_LEFT:
                        // ... handle left action
                                Log.d("test left", "test left");
                                handled = true;
                                break;
                            case KeyEvent.KEYCODE_DPAD_RIGHT:
                        // ... handle right action
                                Log.d("test right", "test right");
                                handled = true;
                                break;
                            }
             //return handled || super.onKeyDown(keyCode, event);
        //}
                        return false;
                    }
                });

                return view;
            }

            private class ViewHolder {
                ImageView imageView;
            }


        }
    }

XML FILES activity main

    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.crouselexample.MainActivity" >

        <com.digitalaria.gama.carousel.Carousel
            android:id="@+id/carousel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"/>

    </RelativeLayout>


for image xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

    <ImageView
        android:id="@+id/itemImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</FrameLayout>

他们有任何解决方案或任何其他方式吗?

4

1 回答 1

0

我会使用leanback支持库。他们有很好的轮播支持(RowsFragment)。Fire TV 支持大多数后仰实施。但是有一些实现不受支持(即搜索片段)。

https://developer.android.com/reference/android/support/v17/leanback/package-summary

亚马逊文档:https ://developer.amazon.com/docs/fire-tv/differences-from-android-tv-development.html#leanback-support-library

卡片演示者示例:

public class CardPresenter extends Presenter {

private static final String TAG = CardPresenter.class.getSimpleName();

private static Context mContext;
private static int CARD_WIDTH = 313;
private static int CARD_HEIGHT = 176;

static class ViewHolder extends Presenter.ViewHolder {
    private Movie mMovie;
    private ImageCardView mCardView;
    private Drawable mDefaultCardImage;

    public ViewHolder(View view) {
        super(view);
        mCardView = (ImageCardView) view;
        mDefaultCardImage = mContext.getResources().getDrawable(R.drawable.movie);
    }

    public void setMovie(Movie m) {
        mMovie = m;
    }

    public Movie getMovie() {
        return mMovie;
    }

    public ImageCardView getCardView() {
        return mCardView;
    }

    public Drawable getDefaultCardImage() {
        return mDefaultCardImage;
    }

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    Log.d(TAG, "onCreateViewHolder");
    mContext = parent.getContext();

    ImageCardView cardView = new ImageCardView(mContext);
    cardView.setFocusable(true);
    cardView.setFocusableInTouchMode(true);
    cardView.setBackgroundColor(mContext.getResources().getColor(R.color.fastlane_background));
    return new ViewHolder(cardView);
}

@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
    Movie movie = (Movie) item;
    ((ViewHolder) viewHolder).setMovie(movie);

    Log.d(TAG, "onBindViewHolder");
    ((ViewHolder) viewHolder).mCardView.setTitleText(movie.getTitle());
    ((ViewHolder) viewHolder).mCardView.setContentText(movie.getStudio());
    ((ViewHolder) viewHolder).mCardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
    ((ViewHolder) viewHolder).mCardView.setMainImage(((ViewHolder) viewHolder).getDefaultCardImage());
}

@Override
public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
    Log.d(TAG, "onUnbindViewHolder");
}

@Override
public void onViewAttachedToWindow(Presenter.ViewHolder viewHolder) {
    // TO DO
}

}

MainFragment 示例:

private void loadRows() {
    mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
    ...

    /* CardPresenter */
    HeaderItem cardPresenterHeader = new HeaderItem(1, "CardPresenter");
    CardPresenter cardPresenter = new CardPresenter();
    ArrayObjectAdapter cardRowAdapter = new ArrayObjectAdapter(cardPresenter);

    for(int i=0; i<10; i++) {
        Movie movie = new Movie();
        movie.setTitle("title" + i);
        movie.setStudio("studio" + i);
        cardRowAdapter.add(movie);
    }
    mRowsAdapter.add(new ListRow(cardPresenterHeader, cardRowAdapter));

     //add code
}
于 2019-03-29T20:06:57.897 回答