Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.github.bumptech.glide:glide:4.5.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
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'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".DetailActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.nd.frt.recentconversation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.nd.frt.recentconversation.model.UserInfo;

public class DetailActivity extends AppCompatActivity {

public static final String PARAM_USER_INFO = "user_info";
public static final String PARAM_USER_INDEX = "user_index";
private UserInfo mUserInfo;
private int mUserIndex;

public static void start(Activity context, int index, UserInfo userInfo, int requestCode) {
Intent starter = new Intent(context, DetailActivity.class);
starter.putExtra(PARAM_USER_INFO, userInfo);
starter.putExtra(PARAM_USER_INDEX, index);
context.startActivityForResult(starter, requestCode);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
ActionBar supportActionBar = getSupportActionBar();
assert supportActionBar != null;
supportActionBar.setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
mUserInfo = ((UserInfo) intent.getSerializableExtra(PARAM_USER_INFO));
mUserIndex = intent.getIntExtra(PARAM_USER_INDEX, 0);
supportActionBar.setTitle(mUserInfo.userName);
supportActionBar.setSubtitle(mUserInfo.content);
final EditText etUserName = findViewById(R.id.etUserName);
ImageView ivAvatar = findViewById(R.id.ivAvatar);
Glide.with(this)
.load(mUserInfo.avatarUrl)
.into(ivAvatar);
findViewById(R.id.btnOk).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mUserInfo.userName = etUserName.getText().toString();
Intent intent1Result = new Intent();
intent1Result.putExtra(PARAM_USER_INFO, mUserInfo);
intent1Result.putExtra(PARAM_USER_INDEX, mUserIndex);
setResult(RESULT_OK, intent1Result);
finish();
}
});

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
43 changes: 39 additions & 4 deletions app/src/main/java/com/nd/frt/recentconversation/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@
package com.nd.frt.recentconversation;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;

import com.nd.frt.recentconversation.adapter.UserAdapter;
import com.nd.frt.recentconversation.model.UserInfo;
import com.nd.frt.recentconversation.service.UserInfoService;

import java.util.List;

public class MainActivity extends AppCompatActivity {

private UserAdapter mUserAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UserInfoService userInfoService = new UserInfoService();
List<UserInfo> userInfos = userInfoService.getUserInfos(this);
UserAdapter userAdapter = new UserAdapter(userInfos);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvRecyclerView);
mUserAdapter = new UserAdapter(userInfos);
RecyclerView recyclerView = findViewById(R.id.rvRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(userAdapter);
recyclerView.setAdapter(mUserAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_add) {
UserInfo userInfo = new UserInfo();
userInfo.userName = "eyyong";
userInfo.content = "kongbai@126.com";
userInfo.avatarUrl = "https://randomuser.me/api/portraits/women/64.jpg";
mUserAdapter.add(userInfo);
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (data != null) {
UserInfo userInfo = (UserInfo) data.getSerializableExtra(DetailActivity.PARAM_USER_INFO);
int index = data.getIntExtra(DetailActivity.PARAM_USER_INDEX, 0);
mUserAdapter.edit(index, userInfo);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.nd.frt.recentconversation.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
Expand All @@ -9,6 +11,7 @@
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.nd.frt.recentconversation.DetailActivity;
import com.nd.frt.recentconversation.R;
import com.nd.frt.recentconversation.model.UserInfo;
import com.nd.frt.recentconversation.viewholder.UserViewHolder;
Expand All @@ -17,7 +20,8 @@

public class UserAdapter extends RecyclerView.Adapter<UserViewHolder> {

public static final String TAG = UserAdapter.class.getSimpleName();
private static final String TAG = UserAdapter.class.getSimpleName();
private static final int REQUSEST_EDIT_USER_INFO = 0x1001;

private List<UserInfo> mUserInfos;

Expand All @@ -35,12 +39,18 @@ public UserViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
}

@Override
public void onBindViewHolder(@NonNull UserViewHolder userViewHolder, int position) {
public void onBindViewHolder(@NonNull UserViewHolder userViewHolder, @SuppressLint("RecyclerView") final int position) {
Log.d(TAG, "onBindViewHolder");
final UserInfo userInfo = mUserInfos.get(position);
Glide.with(userViewHolder.mIvAvatar).load(userInfo.avatarUrl).into(userViewHolder.mIvAvatar);
userViewHolder.mTvUserName.setText(userInfo.userName);
userViewHolder.mTvEmail.setText(userInfo.content);
userViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DetailActivity.start((Activity) v.getContext(),position, userInfo,REQUSEST_EDIT_USER_INFO);
}
});
userViewHolder.mIvAvatar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -53,4 +63,14 @@ public void onClick(View v) {
public int getItemCount() {
return mUserInfos.size();
}

public void add(UserInfo userInfo) {
mUserInfos.add(userInfo);
notifyDataSetChanged();
}

public void edit(int index, UserInfo userInfo) {
mUserInfos.set(index,userInfo);
notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.nd.frt.recentconversation.model;

public class UserInfo {
import java.io.Serializable;

public class UserInfo implements Serializable {

public String avatarUrl;
public String userName;
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable-anydpi/ic_action_add.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#FFFFFF">
<path
android:fillColor="#FF000000"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>
Binary file added app/src/main/res/drawable-hdpi/ic_action_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_action_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_action_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_action_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions app/src/main/res/layout/activity_detail.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center_horizontal"
android:orientation="vertical"
android:padding="10dp"
tools:context=".DetailActivity">

<ImageView
android:id="@+id/ivAvatar"
android:layout_width="96dp"
android:layout_height="96dp"
android:contentDescription="@string/avatar" />

<EditText
android:id="@+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="@string/username"
android:inputType="text"
tools:ignore="Autofill" />

<Button
android:id="@+id/btnOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@android:string/ok" />

</LinearLayout>
11 changes: 11 additions & 0 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/menu_add"
android:icon="@drawable/ic_action_add"
android:title="@string/add"
app:showAsAction="ifRoom" />

</menu>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
<string name="app_name">RecentConversationDemo</string>
<string name="z">dsdsdsdsdsdsdsdsdsa</string>
<string name="hello">Hello</string>
<string name="add">Add</string>
<string name="avatar">avatar</string>
<string name="username">User Name</string>
</resources>