Android
 Computer >> コンピューター >  >> プログラミング >> Android

リサイクラービューを使用したAndroid


リサイクラービューは、リストビューのより高度なバージョンであり、ビューホルダーのデザインパターンに基づいて機能します。 recyclerviewを使用すると、グリッドとアイテムのリストを表示できます。

この例は、年齢とともに学生の名前を表示する美しい学生記録アプリを作成することにより、RecyclerViewを統合する方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 − build.gradleを開き、Recyclerビューライブラリの依存関係を追加します。

apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.example.andy.tutorialspoint"
      minSdkVersion 19
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.android.support:design:28.0.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   implementation 'com.android.support:recyclerview-v7:28.0.0'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
>

ステップ3 −次のコードをres / layout/activity_main.xmlに追加します。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   xmlns:app="https://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   tools:showIn="@layout/activity_main"
   tools:context=".MainActivity">
   <android.support.v7.widget.RecyclerView
      android:id="@+id/recycler_view"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:scrollbars="vertical"
    />
</RelativeLayout>

上記のコードでは、相対的な親レイアウトとしてウィンドウマネージャーにリサイクラービューを追加しました。

ステップ4 −次のコードをsrc / MainActivity.java

に追加します
package com.example.andy.tutorialspoint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
   private RecyclerView recyclerView;
   private StudentAdapter studentAdapter;
   private List studentDataList =new ArrayList<>();
   @TargetApi(Build.VERSION_CODES.O)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      recyclerView = findViewById(R.id.recycler_view);
      studentAdapter=new StudentAdapter(studentDataList);
      RecyclerView.LayoutManager manager=new LinearLayoutManager(this);
      recyclerView.setLayoutManager(manager);
      recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
      recyclerView.setAdapter(studentAdapter);
      StudentDataPrepare();
   }
   private void StudentDataPrepare() {
      studentData data=new studentData("sai",25);
      studentDataList.add(data);
      data=new studentData("sai",25);
      studentDataList.add(data);
      data=new studentData("raghu",20);
      studentDataList.add(data);
      data=new studentData("raj",28);
      studentDataList.add(data);
      data=new studentData("amar",15);
      studentDataList.add(data);
      data=new studentData("bapu",19);
      studentDataList.add(data);
      data=new studentData("chandra",52);
      studentDataList.add(data);
      data=new studentData("deraj",30);
      studentDataList.add(data);
      data=new studentData("eshanth",28);
      studentDataList.add(data);
   }
}

上記のコードでは、リサイクラービューとstudentAdapterを追加しました。その学生アダプターでは、studentDatalistをArrayListとして渡しました。 Studentでは、データリストに学生の名前と年齢が含まれています。

ステップ5 −以下は、変更されたファイルsrc/StudentAdapter.javaの内容です。

package com.example.andy.tutorialspoint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
class StudentAdapter extends RecyclerView.Adapter {
   List studentDataList;
   public StudentAdapter(List studentDataList) {
      this.studentDataList=studentDataList;
   }
   @NonNull
   @Override
   public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
      View itemView = LayoutInflater.from(viewGroup.getContext())
      .inflate(R.layout.student_list_row, viewGroup, false);
      return new MyViewHolder(itemView);
   }
   @Override
   public void onBindViewHolder(MyViewHolder viewHolder, int i) {
      studentData data=studentDataList.get(i);
      viewHolder.name.setText(data.name);
      viewHolder.age.setText(String.valueOf(data.age));
   }
   @Override
   public int getItemCount() {
      return studentDataList.size();
   }
   class MyViewHolder extends RecyclerView.ViewHolder {
      TextView name,age;
      public MyViewHolder(View itemView) {
         super(itemView);
         name=itemView.findViewById(R.id.name);
         age=itemView.findViewById(R.id.age);
      }
   }
}

アダプタクラスには、以下に示す4つのメソッドがあります-

onCreateViewHolder() -ビューホルダーを作成するために使用され、ビューを返します。

onBindViewHolder() -作成されたビューホルダーとバインドします。

getItemCount() -リストのサイズが含まれています。

MyViewHolderクラス -RecyclerView.ViewHolder

によって拡張されるビューホルダー内部クラスです。

ステップ6 −以下は、xml res / layout/student_list_row.xmlの変更されたコンテンツです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:orientation="horizontal" android:layout_width="match_parent"
   android:weightSum="1"
   android:layout_height="wrap_content">
<TextView
   android:id="@+id/name"
   android:layout_width="0dp"
   android:layout_weight="0.5"
   android:gravity="center"
   android:textSize="15sp"
   android:layout_height="100dp" />
   <TextView
      android:id="@+id/age"
      android:layout_width="0dp"
      android:layout_weight="0.5"
      android:gravity="center"
      android:textSize="15sp"
      android:layout_height="100dp" />
</LinearLayout>

上記のリストアイテムビューでは、名前と年齢の2つのテキストビューを作成しました。

ステップ7 −変更されたファイルsrc/studentData.javaの内容は次のとおりです。

package com.example.andy.tutorialspoint;
class studentData {
   String name;
   int age;
   public studentData(String name, int age) {
      this.name=name;
      this.age=age;
   }
}

上記のコードでは、学生データオブジェクトについて通知します。アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-

リサイクラービューを使用したAndroid

下にスクロールします。以下のような結果が表示されます-

リサイクラービューを使用したAndroid


  1. バイブレーション、サウンド、アクション、ビッグビュースタイルのAndroid通知の例

    この例は、バイブレーション、サウンド、アクション、ビッグビュースタイルを使用したAndroid通知の例について示しています ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <? xml version= "1.0" encoding= "utf-8" ?> <android.support.c

  2. コントローラをサポートする最高のiOSおよびAndroidゲーム

    数年前のiOSデバイス向けの公式ゲームコントローラーサポートの導入は、Appleにとって根本的な変化でした。ゲームはタッチコントロールを使用できるのと同じくらい優れていますが、コントローラーのサポートにより、まったく新しいレベルのプレイアビリティが追加されます。もちろん、Androidは何年もの間ゲームコントローラーをサポートしてきました。 このリストでは、ここでは、コントローラーを使用して実際に次のレベルに進むゲーム、またはコントローラーなしでプレイするのが難しいゲームに焦点を当てます。ですから、お気に入りのパズルゲームやポイントアンドクリックの冒険が効果を発揮することを期待しないでくださ