package com.example.superb.zidian;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
//import android.support.v7.app.LinearLayoutManager;
//import android.support.v7.app.RecyclerView;
//import com.haocent.android.recyclerview.R;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
- 绾靛悜甯冨眬 鐨?Activity
- Created by Tnno Wu on 2018/03/05.
*/
public class VerticalActivity extends AppCompatActivity {
public static final String WORD_KEY = “f98139cfac1da1eed4810e33d0d51a11”;//鍦ㄨ仛鍚堟暟鎹噷闈㈢敵璇风殑浜у搧绉橀挜锛屾嫢鏈夌閽ユ墠鑳藉幓璁块棶杩欎釜缃戝潃
private EditText editword;
private TextView result,result1,result2;
private Handler handler;
private Button button;
//private static final String TAG = VerticalActivity.class.getSimpleName();
private List<String> mList = new ArrayList<>();
String pinyin,tongyi,fanyi,chengyujs,from_,yinzhengjs;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vertical_activity);
initialize();
initData();
//initView();
initView();
}
private void initialize() {
editword = (EditText) findViewById(R.id.edit_word);
result = (TextView) findViewById(R.id.result);
button= (Button) findViewById(R.id.button);
}
private void initData() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button:
Log.e("ssssss", "onsssssssssssssssssssssss");
final String edit_result=editword.getText().toString();
mList = new ArrayList<>();
handler= new Handler() {
@Override
public void handleMessage(final Message msg) {//handler鍔燭hread缁勬垚寮傛绾跨▼璁块棶缃戠粶
super.handleMessage(msg);
switch (msg.what) {
case 1:
if (edit_result!=null) {
String url= "http://v.juhe.cn/chengyu/query?key="+ WORD_KEY+"&word="+edit_result;
RxVolley.get(url, new HttpCallback() {
@Override
public void onSuccess(String t) {//瀛楃涓叉槸涓€涓猨son鏍煎紡鐨勫瓧绗︿覆锛孯xVolley鏄闂綉缁滅殑涓€涓鏋讹紝鏄坊鍔犵殑渚濊禆
super.onSuccess(t);
mList.add(parseJson(t));
//+’\n’+tongyi+’\n’+fanyi+’\n’+chengyujs+’\n’+from_+’\n’+yinzhengjs+’\n’
mList.add("鍚屼箟璇嶏細 "+tongyi+’\n’+"鍙嶄箟璇嶏細 "+fanyi);
mList.add(“鍚箟锛?”+chengyujs);
mList.add(“搴斿緛瑙i噴锛?”+yinzhengjs);
mList.add(“鍑哄锛?”+from_);
result.setText(from_);
initView();
}
});
break;
}else{
Toast.makeText(VerticalActivity.this, "杈撳叆鐨勬垚璇负绌?+edit_result, Toast.LENGTH_SHORT).show();
}
}
}
};
new Thread(){
@Override
public void run() {
super.run();
Message message=new Message();
message.what=1;
handler.sendMessage(message);
}
}.start();
break;
}
}
});
}
private String parseJson(String t) {//瑙f瀽json瀛楃涓?
String s="";
try {
JSONObject jsonObject = new JSONObject(t);
JSONObject result = jsonObject.getJSONObject("result");
pinyin=result.getString("pinyin");
chengyujs = result.getString("chengyujs");
from_=result.getString("from_");
yinzhengjs=result.getString("yinzhengjs");
s=chengyujs+from_+yinzhengjs;
tongyi = result.getString("tongyi");
// s=s+tongyi;
s=chengyujs+from_+yinzhengjs+tongyi;
fanyi = result.getString("fanyi");
s=pinyin;
//s=s+fanyi;
//s=chengyujs;
} catch (JSONException e) {
e.printStackTrace();
}
return s;
}
private void initView() {
VerticalAdapter adapter = new VerticalAdapter(this);
RecyclerView rcvVertical = findViewById(R.id.rcv_vertical);
LinearLayoutManager managerVertical = new LinearLayoutManager(this);
managerVertical.setOrientation(LinearLayoutManager.VERTICAL);
// 涔熷彲浠ョ洿鎺ュ啓鎴愶細
// rcvVertical.setLayoutManager(new LinearLayoutManager(this));
rcvVertical.setLayoutManager(managerVertical);
rcvVertical.setHasFixedSize(true);
rcvVertical.setAdapter(adapter);
adapter.setVerticalDataList(mList);
}
}
package com.example.superb.zidian;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.*;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
//import com.haocent.android.recyclerview.R;
import java.util.ArrayList;
import java.util.List;
/**
- 绾靛悜甯冨眬 鐨?Adapter
- Created by Tnno Wu on 2018/03/05.
*/
public class VerticalAdapter extends RecyclerView.Adapter<VerticalAdapter.VerticalViewHolder> {
private static final String TAG = VerticalAdapter.class.getSimpleName();
private Context mContext;
private List<String> mList = new ArrayList<>();
public VerticalAdapter(Context context) {
mContext = context;
}
public void setVerticalDataList(List<String> list) {
Log.d(TAG, "setVerticalDataList: " + list.size());
mList = list;
notifyDataSetChanged();
}
@NonNull
@Override
public VerticalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.vertical_recycle_item, parent, false);
return new VerticalViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull VerticalViewHolder holder, int position) {
holder.tvNum.setText(position + 1 + "");
holder.tvContent.setText(mList.get(position));
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
public class VerticalViewHolder extends RecyclerView.ViewHolder {
TextView tvNum, tvContent;
public VerticalViewHolder(View itemView) {
super(itemView);
tvNum = itemView.findViewById(R.id.tv_num);
tvContent = itemView.findViewById(R.id.tv_content);
}
}
}
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="70dp"
android:layout_height="60dp"
android:background="@drawable/shousuobg3"/>
<EditText
android:id="@+id/edit_word"
android:layout_width="211dp"
android:layout_height="wrap_content"
android:textSize="15sp" />
<Button
android:id="@+id/button"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/vns"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rcv_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:overScrollMode="never"
android:scrollbars="none" >
</android.support.v7.widget.RecyclerView>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="180dp"
android:textSize="16sp"
/>
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
tools:text="1"
android:textColor="@android:color/black"/>
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="8dp"
tools:text="鍐呭"
android:textColor="@android:color/black"
/>