This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1003 from Xterisk/master
- Loading branch information
Showing
29 changed files
with
953 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.