我从JSON获取数据并以RecyclerView布局显示。我的textview显示整个链接。例如,我的TextView是安装的,链接是alerts
。在RecyclerView中,它应该只显示http://mydomin.com?id=2054656
文本,当它被点击时,应该转到URL。我试过下面的代码。
install
它工作正常,但显示整个链接而不是文本。我希望你明白。
将textview更改为按钮后,请查看我的完整代码
Viewholder.ImageUrlNameView.setText(getDataAdapter1.getImageUrlName());
ImageUrlNameView = (TextView) itemView.findViewById(tvAppLink) ;
ImageUrlNameView.setMovementMethod(LinkMovementMethod.getInstance());
低于错误
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import java.util.List;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<GetDataAdapter> getDataAdapter;
ImageLoader imageLoader1;
public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {
GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();
imageLoader1.get(getDataAdapter1.getImageServerUrl(),
ImageLoader.getImageListener(
Viewholder.networkImageView,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);
Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);
Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());
Viewholder.ImageUrlNameView.setText(getDataAdapter1.getImageUrlName());
}
@Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public NetworkImageView networkImageView ;
public TextView ImageTitleNameView;
public Button ImageUrlNameView ;
public ViewHolder(View itemView) {
super(itemView);
networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1) ;
ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item);
//ImageUrlNameView = (Button) itemView.findViewById(R.id.bAppUrl) ;
//ImageUrlNameView.setMovementMethod(LinkMovementMethod.getInstance());
//Linkify.addLinks(ImageUrlNameView, Linkify.WEB_URLS);
itemView.findViewById(R.id.bAppUrl).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "ImageUrlNameView";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
}
}
请检查图像我想要获得但得到错误
答案 0 :(得分:1)
通过这种方式将点击逻辑更改为onBindViewHolder
,您拥有当前对象的网址:
@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {
GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();
imageLoader1.get(getDataAdapter1.getImageServerUrl(),
ImageLoader.getImageListener(
Viewholder.networkImageView,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);
Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);
Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());
Viewholder.ImageUrlNameView.setText("Install");
Viewholder.ImageUrlNameView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = getDataAdapter1.getImageUrlName();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public NetworkImageView networkImageView ;
public TextView ImageTitleNameView;
public Button ImageUrlNameView ;
public ViewHolder(View itemView) {
super(itemView);
networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1) ;
ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item);
ImageUrlNameView = (Button) itemView.findViewById(R.id.bAppUrl) ;
//ImageUrlNameView.setMovementMethod(LinkMovementMethod.getInstance());
//Linkify.addLinks(ImageUrlNameView, Linkify.WEB_URLS);
}
}
答案 1 :(得分:0)
方法1: 尝试这对我有用 - 1) String installString =“install”; String hyperlinkString =“Whatever string”+ installString +“text”; SpannableString hyperlinkText = makeHyperlinkText(hyperlinkString,installString); textView.setText(hyperlinkText); textView.setMovementMethod(LinkMovementMethod.getInstance());
2)功能在这里:
private SpannableString makeHyperlinkText(String hyperlinkString, String textToMakeHyperlink) {
SpannableString content = new SpannableString(hyperlinkString);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Intent webviewIntent = new Intent(YourCurrentActivity.this, WebviewActivity.class);
webviewIntent.putExtra("URL", your_url_here); //EDIT
startActivity(webviewIntent);
}
};
int startIndex = hyperlinkString.indexOf(textToMakeHyperlink);
int endIndex = startIndex + textToMakeHyperlink.length();
content.setSpan(clickableSpan, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return content;
}
//编辑:在
下添加这些文件3)WebviewActivity类在这里 -
public class WebviewActivity extends AppCompactActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_webview_layout);
String url = getIntent().getStringExtra("URL");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(url);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
4)Webview Layour类(根据需要自定义):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.massmutual.css.ui.loan.LoanDisclosureWebView">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
方法2: 您可以更改传递url字符串的方式。它需要是HTML格式。 例如:
<a href="www.google.com">Click here for google.</a>
所以我认为你的文字会是这样的:
String url = "http://mydomin.com?id=2054656";
String text = "install";
String hyperlinkText = "<a href=\"" + url + "\""> + text + "</a>";
让我知道这些是否适合您。