Android Studioが作成した最初のAndroid TVプロジェクトの解析

25809 ワード

突然ブログを書こうと思って、このブログが生まれた.ソースコードを理解していないところも多く、注文して見ていないので、その後も修正し続けます.もしあなたに少し助けがあれば、あるいはあなたが神馬の考えを持っているか、困惑しているかを伝言することができます.
いつもの習慣で、一つのプロジェクトを手に入れると、私は先にリストファイルを見に行くので、まずリストファイルを分析しましょう.
<uses-permission android:name="android.permission.INTERNET" />   //         
<uses-permission android:name="android.permission.RECORD_AUDIO" />  //           

<uses-feature  android:name="android.hardware.touchscreen"  android:required="false" />           //           ,          
<uses-feature  android:name="android.software.leanback"  android:required="true" />     //    TV      ,     
 
  
<activity  android:name=".MainActivity"  android:banner="@drawable/app_icon_your_company"  android:icon="@drawable/app_icon_your_company"  android:label="@string/app_name"  android:logo="@drawable/app_icon_your_company"  android:screenOrientation="landscape">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />    
        <category android:name="android.intent.category.LEANBACK_LAUNCHER" />  //              ,            TV  ,      ,     TV  ,              
    intent-filter>
activity>

それから私はMainActivityを開けて、中が空いていることに気づきました.その後、MainActivityのリソースファイルを開きました.
<fragment xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:id="@+id/main_browse_fragment"  android:name="com.example.administrator.myfirst.MainFragment" //      ,            。  android:layout_width="match_parent"  android:layout_height="match_parent"  tools:context="com.example.administrator.myfirst.MainActivity"  tools:deviceIds="tv"  tools:ignore="MergeRootFrame" />

そしてMainFragmentを開くことができます.中には4つの方法があります.私たちは一緒に分析します.
public void onActivityCreated(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate");
    super.onActivityCreated(savedInstanceState);

    prepareBackgroundManager();

    setupUIElements();

    loadRows();

    setupEventListeners();
}
                ,   。
private void prepareBackgroundManager() {

    mBackgroundManager = BackgroundManager.getInstance(getActivity());
    mBackgroundManager.attach(getActivity().getWindow());
    mDefaultBackground = getResources().getDrawable(R.drawable.default_background);
    mMetrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
}
private void setupUIElements() {
    // setBadgeDrawable(getActivity().getResources().getDrawable(  // R.drawable.videos_by_google_banner));  setTitle(getString(R.string.browse_title)); // Badge, when set, takes precedent  // over title  setHeadersState(HEADERS_ENABLED);
    setHeadersTransitionOnBackEnabled(false); //              。

    // set fastLane (or headers) background color  setBrandColor(getResources().getColor(R.color.background_gradient_start));  //          
    // set search icon color  setSearchAffordanceColor(getResources().getColor(R.color.search_opaque));
}
               
 
  
private void loadRows() {
    List list = MovieList.setupMovies();  //Movie         Bean。setupMovies()   movie     

    mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());  //        
    CardPresenter cardPresenter = new CardPresenter();         //            

    int i;
    for (i = 0; i < NUM_ROWS; i++) {    

        if (i != 0) {
            Collections.shuffle(list);   //  list    
        }
        ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);  //      Persenter   ,Persenter         ,   Adapter
        for (int j = 0; j < NUM_COLS; j++) {
            listRowAdapter.add(list.get(j % 5));    //  Movie,       15   
        }
        HeaderItem header = new HeaderItem(i, MovieList.MOVIE_CATEGORY[i]);  //         
        mRowsAdapter.add(new ListRow(header, listRowAdapter));   //listRowAdapter        
 // new ListRow(header, listRowAdapter)                 }  
  HeaderItem gridHeader = new HeaderItem(i, "PREFERENCES");   
 GridItemPresenter mGridPresenter = new GridItemPresenter();  
  ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(mGridPresenter); //      Presenter 
gridRowAdapter.add(getResources().getString(R.string.grid_view));   
 gridRowAdapter.add(getString(R.string.error_fragment));   
 gridRowAdapter.add(getResources().getString(R.string.personal_settings));    
mRowsAdapter.add(new ListRow(gridHeader, gridRowAdapter));  //             
 setAdapter(mRowsAdapter); //   mRowsAdapter  }
public class CardPresenter extends Presenter {
    private static final String TAG = "CardPresenter";

    private static final int CARD_WIDTH = 313;
    private static final int CARD_HEIGHT = 176;
    private static int sSelectedBackgroundColor;
    private static int sDefaultBackgroundColor;
    private Drawable mDefaultCardImage;

    private static void updateCardBackgroundColor(ImageCardView view, boolean selected) {
        int color = selected ? sSelectedBackgroundColor : sDefaultBackgroundColor;
        // Both background colors should be set because the view's background is temporarily visible  // during animations.  view.setBackgroundColor(color);
        view.findViewById(R.id.info_field).setBackgroundColor(color);
    }

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

        sDefaultBackgroundColor = parent.getResources().getColor(R.color.default_background);
        sSelectedBackgroundColor = parent.getResources().getColor(R.color.selected_background);
        /*  * This template uses a default image in res/drawable, but the general case for Android TV  * will require your resources in xhdpi. For more information, see  * https://developer.android.com/training/tv/start/layouts.html#density-resources  */  mDefaultCardImage = parent.getResources().getDrawable(R.drawable.movie);

        ImageCardView cardView = new ImageCardView(parent.getContext()) {
            @Override
            public void setSelected(boolean selected) {
                updateCardBackgroundColor(this, selected);  //     ,            ,       
                super.setSelected(selected);
            }
        };
        cardView.setFocusable(true);
        cardView.setFocusableInTouchMode(true);
        updateCardBackgroundColor(cardView, false);
        return new ViewHolder(cardView);
    }

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

        Log.d(TAG, "onBindViewHolder");
        if (movie.getCardImageUrl() != null) {
            cardView.setTitleText(movie.getTitle());
            cardView.setContentText(movie.getStudio());
            cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
            Glide.with(viewHolder.view.getContext())
                    .load(movie.getCardImageUrl())
                    .centerCrop()
                    .error(mDefaultCardImage)
                    .into(cardView.getMainImageView());
        }
    }

    @Override
    public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
        Log.d(TAG, "onUnbindViewHolder");
        ImageCardView cardView = (ImageCardView) viewHolder.view;
        // Remove references to images so that the garbage collector can free up memory  cardView.setBadgeImage(null);
        cardView.setMainImage(null);
    }
}
         
private void setupEventListeners() {
    setOnSearchClickedListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "Implement your own in-app search", Toast.LENGTH_LONG)
                    .show();
        }
    });

    setOnItemViewClickedListener(new ItemViewClickedListener());
    setOnItemViewSelectedListener(new ItemViewSelectedListener());
}
今回はここまで