Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1003 from Xterisk/master
Browse files Browse the repository at this point in the history
#4 #11 第4次实验及实验报告
  • Loading branch information
zengsn authored Nov 16, 2020
2 parents 068cf62 + 2e0b0b1 commit d655f3b
Show file tree
Hide file tree
Showing 29 changed files with 953 additions and 61 deletions.
2 changes: 0 additions & 2 deletions students/net1814080903211/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
</intent-filter>
</activity>

<activity android:name=".ProfileActivity" />
<activity android:name=".BrowseActivity" />
</application>

</manifest>
41 changes: 41 additions & 0 deletions students/net1814080903211/ChatObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package edu.hzuapps.androidlabs.net1814080903211.components;

import androidx.annotation.Nullable;

public class ChatObject {
String name;
String type;
@Nullable
String extraInfo;

public ChatObject(String name, String type, @Nullable String extraInfo) {
this.name = name;
this.type = type;
this.extraInfo = extraInfo;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

@Nullable
public String getExtraInfo() {
return extraInfo;
}

public void setExtraInfo(@Nullable String extraInfo) {
this.extraInfo = extraInfo;
}
}
102 changes: 102 additions & 0 deletions students/net1814080903211/HomeFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package edu.hzuapps.androidlabs.net1814080903211.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import edu.hzuapps.androidlabs.net1814080903211.components.ListSeparator;
import edu.hzuapps.androidlabs.net1814080903211.databinding.FragmentHomeBinding;
import edu.hzuapps.androidlabs.net1814080903211.components.ChatObject;
import edu.hzuapps.androidlabs.net1814080903211.databinding.ChatListItemBinding;


class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ChatListItemHolder> {
private final ArrayList<ChatObject> objectList;

public ChatListAdapter(ArrayList<ChatObject> list) {
super();
objectList = list;
}

static class ChatListItemHolder extends RecyclerView.ViewHolder {
private final ChatListItemBinding binding;

public ChatListItemHolder(@NonNull @NotNull ChatListItemBinding binding) {
super(binding.getRoot());

this.binding = binding;
}

public void setBinding(ChatObject object) {
binding.setObject(object);
binding.executePendingBindings();
}
}

@NonNull
@NotNull
@Override
public ChatListItemHolder onCreateViewHolder(@NonNull @NotNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
ChatListItemBinding binding = ChatListItemBinding.inflate(inflater, viewGroup, false);
return new ChatListItemHolder(binding);
}

@Override
public void onBindViewHolder(@NonNull @NotNull ChatListItemHolder chatListItemHolder, int i) {
chatListItemHolder.setBinding(objectList.get(i));
}

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

public class HomeFragment extends Fragment {

private FragmentHomeBinding binding;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);

binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();

RecyclerView recyclerView = binding.chatList;
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new ListSeparator());

homeViewModel.getChatObjects().observe(getViewLifecycleOwner(),
new Observer<ArrayList<ChatObject>>() {
@Override
public void onChanged(@Nullable ArrayList<ChatObject> s) {
ChatListAdapter adapter = new ChatListAdapter(s);
recyclerView.setAdapter(adapter);
}
});
return root;
}

@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
37 changes: 37 additions & 0 deletions students/net1814080903211/HomeViewModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package edu.hzuapps.androidlabs.net1814080903211.ui.home;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.ArrayList;
import java.util.Arrays;

import edu.hzuapps.androidlabs.net1814080903211.components.ChatObject;

public class HomeViewModel extends ViewModel {

private final MutableLiveData<ArrayList<ChatObject>> mChatObjects;

public HomeViewModel() {
mChatObjects = new MutableLiveData<>();
ChatObject[] raw_list = new ChatObject[]{
new ChatObject("Test Item Person", "Person", "昨天晚上你去哪里了?"),
new ChatObject("Test Item Room", "Room", "这家店还行吧。"),
new ChatObject("Test Person 2", "Person", null),
new ChatObject("Test Person 3", "Person", null),
new ChatObject("Test Room 2", "Room", "今天有个通知"),
new ChatObject("Test Room 3", "Room", null),
new ChatObject("Test Person 4", "Person", null),
new ChatObject("Test Room 4", "Room", "有没有你感兴趣的?"),
new ChatObject("Test Person 5", "Person", null),
};
ArrayList<ChatObject> list = new ArrayList<>(Arrays.asList(raw_list));

mChatObjects.setValue(list);
}

public LiveData<ArrayList<ChatObject>> getChatObjects() {
return mChatObjects;
}
}
30 changes: 30 additions & 0 deletions students/net1814080903211/ListSeparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package edu.hzuapps.androidlabs.net1814080903211.components;

import android.graphics.Rect;
import android.view.View;

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

import org.jetbrains.annotations.NotNull;

public class ListSeparator extends RecyclerView.ItemDecoration {

private static int margin = 12;

@Override
public void getItemOffsets(@NonNull @NotNull Rect outRect, @NonNull @NotNull View view,
@NonNull @NotNull RecyclerView parent, @NonNull @NotNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);

final float scale = parent.getResources().getDisplayMetrics().density;
final int margin_dp = (int) (margin * scale + 0.5f);

if (parent.getChildLayoutPosition(view) == 0) {
outRect.top = margin_dp;
}
outRect.bottom = margin_dp;
outRect.left = margin_dp;
outRect.right = margin_dp;
}
}
23 changes: 3 additions & 20 deletions students/net1814080903211/Net1814080903211Activity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package edu.hzuapps.androidlabs.net1814080903211;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.widget.Button;

import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.navigation.NavController;
Expand All @@ -21,13 +17,12 @@
public class Net1814080903211Activity extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

binding = ActivityMainBinding.inflate(getLayoutInflater());
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

setSupportActionBar(binding.appBarMain.toolbar);
Expand All @@ -36,8 +31,8 @@ protected void onCreate(Bundle savedInstanceState) {
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
R.id.nav_home, R.id.nav_search, R.id.nav_profile)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
Expand All @@ -51,18 +46,6 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

public void jumpToProfile(View v)
{
Intent newActivity = new Intent(this, ProfileActivity.class);
startActivity(newActivity);
}

public void jumpToBrowse(View v)
{
Intent newActivity = new Intent(this, BrowseActivity.class);
startActivity(newActivity);
}

@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
Expand Down
60 changes: 60 additions & 0 deletions students/net1814080903211/ProfileFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package edu.hzuapps.androidlabs.net1814080903211.ui.profile;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

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

import edu.hzuapps.androidlabs.net1814080903211.R;
import edu.hzuapps.androidlabs.net1814080903211.databinding.FragmentProfileBinding;

public class ProfileFragment extends Fragment {

private FragmentProfileBinding binding;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
ProfileViewModel profileViewModel = new ViewModelProvider(this).get(ProfileViewModel.class);

binding = FragmentProfileBinding.inflate(inflater, container, false);
View root = binding.getRoot();

Spinner sex_spinner = binding.sexualSpinner;
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(root.getContext(),
R.array.profile_sexual_selections, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
sex_spinner.setAdapter(adapter);

Spinner age_spinner = binding.ageSpinner;
ArrayList<Integer> ages = new ArrayList<>();
for (int i = 1; i < 101; i++) {
ages.add(i);
}
ArrayAdapter<Integer> age_adapter = new ArrayAdapter<>(root.getContext(),
android.R.layout.simple_spinner_item, ages);
age_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
age_spinner.setAdapter(age_adapter);
return root;
}

@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
19 changes: 19 additions & 0 deletions students/net1814080903211/ProfileViewModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package edu.hzuapps.androidlabs.net1814080903211.ui.profile;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class ProfileViewModel extends ViewModel {

private MutableLiveData<String> mText;

public ProfileViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is slideshow fragment");
}

public LiveData<String> getText() {
return mText;
}
}
Loading

0 comments on commit d655f3b

Please sign in to comment.