Я новичок в Android и ниже - мой класс RecyclerAdapter
, который распространяется на RecyclerView
. Я попытался установить onClicklistener
в моем RecyclerViewHolder
, но мне не удалось вызвать новое действие, потому что я, похоже, не знаю, как получить ссылку на контекст от адаптера. Любая помощь будет оценена по достоинству.Как обрабатывать клики внутри RecyclerViewHolder
public class TabFragmentNews extends Fragment {
private static final String ARG_EXAMPLE = "this_is_a_constant";
private String example_data;
private RecyclerView recyclerView;
ProgressBar progressBar;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news, container, false);
progressBar = (ProgressBar) view.findViewById(R.id.progressBarNews);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_news);
//Executing The Async Task Class Here
newsupdate();
return view;
}
//Method For Executing The Async Task
public void newsupdate() {
new BackgroundTask(getActivity()).execute();
}
//Checking for internet/network connection here
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
example_data = getArguments().getString(ARG_EXAMPLE);
Log.i("Fragment created with ", example_data);
}
public static TabFragmentNews newInstance(String example_argmument) {
TabFragmentNews tabFragmentNews = new TabFragmentNews();
Bundle args = new Bundle();
args.putString(ARG_EXAMPLE, example_argmument);
tabFragmentNews.setArguments(args);
return tabFragmentNews;
}
//AsynTask Class for Downloading News Information
class BackgroundTask extends AsyncTask<Void, News, Void> {
Context ctx;
Activity activity;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ProgressDialog progressDialog;
ArrayList<News> arrayList = new ArrayList<>();
public BackgroundTask(Context ctx) {
this.ctx = ctx;
activity = (Activity) ctx;
}
String json_string = "http://doubleclickug.com/app/newtest.php";
@Override
protected void onPreExecute() {
layoutManager = new LinearLayoutManager(ctx);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new RecyclerAdapter(getContext(),arrayList);
recyclerView.setAdapter(adapter);
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL(json_string);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
httpURLConnection.disconnect();
String json_string = stringBuilder.toString().trim();
JSONObject jsonObject = new JSONObject(json_string);
JSONArray jsonArray = jsonObject.getJSONArray("server_response");
int count = 0;
while (count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
count++;
News news = new News(JO.getString("details"));
publishProgress(news);
//Thread.sleep(100);
}
Log.d("JSON STRING", json_string);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
//} catch (InterruptedException e) {
// e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(News... values) {
arrayList.add(values[0]);
adapter.notifyDataSetChanged();
}
@Override
protected void onPostExecute(Void aVoid) {
//progressDialog.dismiss();
progressBar.setVisibility(View.GONE);
}
}
//RecyclerAdapter Class for populating recycler view with Information
class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
ArrayList<News> arrayList = new ArrayList<>();
public static final int TYPE_HEAD = 0;
public static final int TYPE_LIST = 1;
Context context;
public RecyclerAdapter(Context context,ArrayList<News> arrayList) {
this.arrayList = arrayList;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEAD) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_header, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view, viewType);
return recyclerViewHolder;
} else if (viewType == TYPE_LIST) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_row_layout, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view, viewType);
return recyclerViewHolder;
}
return null;
}
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
if (holder.viewType == TYPE_LIST) {
final News news = arrayList.get(position - 1);
holder.Details.setText(news.getDetails());
}
}
@Override
public int getItemCount() {
return arrayList.size() + 1;
}
//RecyclerView Holder Class
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView Details;
int viewType;
public RecyclerViewHolder(View view, int viewType) {
super(view);
view.setOnClickListener(this);
if (viewType == TYPE_LIST) {
Details = (TextView) itemView.findViewById(R.id.team);
this.viewType = TYPE_LIST;
} else if (viewType == TYPE_HEAD) {
this.viewType = TYPE_HEAD;
}
}
//handling Clicks of news elements here
@Override
public void onClick(View v) {
//How Do I Call an Activity???
context.startActivity(new Intent(context,NewsActivity.class));
}
}
@Override
public int getItemViewType(int position) {
if (position == 0)
return TYPE_HEAD;
return TYPE_LIST;
}
}
После редактирования коды выше: -The приложение не перейти к другой деятельности, при нажатии кнопки он расправляется
стека пожалуйста .. – DJphy