我有一个片段,可以使用Volley从我的服务器获取学生词汇表的列表。单词列表显示在RecyclerView中,顶部带有FAB。如何连接我的FAB,以便在按下时,CardView将显示为在RecylerView上弹出。这样做的目的是使用CardView作为表单与另一个脚本进行通信,该脚本允许用户输入要添加到列表中的单词。当在CardView上单击该按钮时,它应该运行POST脚本并消失。这在单个片段中是否可行?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.words_ui, container, false);
ButterKnife.bind(this, view);
wordRecyclerView = (RecyclerView) view.findViewById(R.id.wordsRecyclerView);
wordRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
wordRecyclerView.addItemDecoration(new VerticalSpaceItemDecoration(VERTICAL_ITEM_SPACE));
mWordAdapter = new WordAdapter(getActivity());
wordRecyclerView.setAdapter(mWordAdapter);
TextView clientTextView = (TextView) view.findViewById(R.id.clientText);
Bundle args = getActivity().getIntent().getExtras();
final String username = args.getString("USER_NAME");
clientTextView.setText("Word List For: " + username);
FloatingActionButton wordButton = (FloatingActionButton) view.findViewById(R.id.wordFloatingButton);
wordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Button was clicked", Toast.LENGTH_LONG).show();
}
});
sendRequest();
return view;
}
private void sendRequest() {
Bundle args = getActivity().getIntent().getExtras();
final String username = args.getString("USER_NAME");
final String URL = JSON_URL + username;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
mWordArrayList = parseJSONResponse(response);
mWordAdapter.setWordArrayList(mWordArrayList);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(request);
}
public ArrayList<Word> parseJSONResponse(JSONObject response) {
ArrayList<Word> mWordArrayList = new ArrayList<Word>();
if (response == null || response.length() == 0) {
}
try {
JSONArray jsonArray = response.getJSONArray(JSON_ARRAY);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject usersWords = jsonArray.getJSONObject(i);
String words = usersWords.getString(KEY_WORD);
String addbys = usersWords.getString(KEY_ADDBY);
Word word = new Word(words, addbys);
word.setWord(words);
word.setAddby("Added by: " + addbys);
mWordArrayList.add(word);
}
} catch (JSONException e) {
e.printStackTrace();
}
return mWordArrayList;
}
答案 0 :(得分:0)
我想我可能找到了一个临时解决方案。我很想知道是否有人可以实施上述内容或告诉我更好的方法。
我最终做的只是创建一个新的Activity AddWord.class并从onClickListener调用它。然后我创建一个视图和这样的自定义主题......
<style name="AppTheme.CustomTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowCloseOnTouchOutside">true</item>
</style>
单击FAB时,视图会在RecyclerView顶部弹出,后者仍然可见。用户可以填写表单并将单词添加到列表中,然后返回RecyclerView。
不确定这是多么昂贵,所以如果有人有更好的选择让我知道!