Skip to content

Commit

Permalink
add RecyclerView
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjobs committed Apr 30, 2020
1 parent f9bde91 commit 49e3783
Show file tree
Hide file tree
Showing 45 changed files with 1,050 additions and 0 deletions.
14 changes: 14 additions & 0 deletions AdvancedView/RecyclerViewTest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
1 change: 1 addition & 0 deletions AdvancedView/RecyclerViewTest/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions AdvancedView/RecyclerViewTest/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions AdvancedView/RecyclerViewTest/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions AdvancedView/RecyclerViewTest/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions AdvancedView/RecyclerViewTest/.idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions AdvancedView/RecyclerViewTest/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions AdvancedView/RecyclerViewTest/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions AdvancedView/RecyclerViewTest/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

defaultConfig {
applicationId "jssvc.edu.cn.recyclerview2"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
21 changes: 21 additions & 0 deletions AdvancedView/RecyclerViewTest/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package jssvc.edu.cn.recyclerview2;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

assertEquals("jssvc.edu.cn.recyclerview2", appContext.getPackageName());
}
}
21 changes: 21 additions & 0 deletions AdvancedView/RecyclerViewTest/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jssvc.edu.cn.recyclerview2">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package jssvc.edu.cn.recyclerview2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = findViewById(R.id.recyclerView);

List<Person> personList = new ArrayList<>();
Person person1 = new Person("zhangsan","15512345678",R.drawable.ic_android_black_24dp);
Person person2 = new Person("lisi","18812345678",R.drawable.ic_directions_run_black_24dp);
personList.add(person1);
personList.add(person2);

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(RecyclerView.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);

MyAdapter myAdapter = new MyAdapter(personList);

recyclerView.setAdapter(myAdapter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package jssvc.edu.cn.recyclerview2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Person> personList;

public MyAdapter(List<Person> personList) {
this.personList = personList;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(itemView);
return myViewHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Person person = personList.get(position);
holder.nameText.setText(person.getName());
holder.phoneText.setText(person.getPhone());
holder.head.setImageResource(person.getHeadId());
}

@Override
public int getItemCount() {
return personList.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {
TextView nameText;
TextView phoneText;
ImageView head;

public MyViewHolder(View itemView) {
super(itemView);
this.nameText = itemView.findViewById(R.id.textView);
this.phoneText = itemView.findViewById(R.id.textView2);
this.head = itemView.findViewById(R.id.imageView);
}
}
}
Loading

0 comments on commit 49e3783

Please sign in to comment.