gpt4 book ai didi

android - RecyclerView TextView 中的超链接

转载 作者:行者123 更新时间:2023-11-30 00:55:19 26 4
gpt4 key购买 nike

我正在从 JSON 中获取数据并显示在 RecyclerView 布局中。我的 TextView 显示整个链接。例如,我的 TextView 已安装,链接为 http://mydomin.com?id=2054656。在 RecyclerView 中,它应该只显示 install 文本,当它被点击时应该转到 URL。我试过下面的代码。

Viewholder.ImageUrlNameView.setText(getDataAdapter1.getImageUrlName());

ImageUrlNameView = (TextView) itemView.findViewById(tvAppLink) ;

ImageUrlNameView.setMovementMethod(LinkMovementMethod.getInstance());

它工作正常,但显示的是整个链接而不是文本。我希望你明白。

请在将textview 更改为button 后查看我的完整代码

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);
}
});


}


}

}

低于错误

10-27 20:58:43.896 12462-12462/com.android_examples.recyclerviewimagelistview_android_examplescom E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.android_examples.recyclerviewimagelistview_android_examplescom.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:73)
at com.android_examples.recyclerviewimagelistview_android_examplescom.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:29)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
10-27 21:03:43.956 12462-12462/? I/Process: Sending signal. PID: 12462 SIG: 9

请检查我想要获取的图像但出现错误

enter image description here

最佳答案

把点击的逻辑改成onBindViewHolder这样,你就有了当前对象的URL:

 @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);




}


}

关于android - RecyclerView TextView 中的超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40269780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com