无法在其适配器 Android 应用开发中调用 Fragment 的功能
Posted
技术标签:
【中文标题】无法在其适配器 Android 应用开发中调用 Fragment 的功能【英文标题】:Not able to call the Fragment's function in its Adapter, Android App Development 【发布时间】:2015-11-28 22:16:48 【问题描述】:我正在制作一个 android 应用程序,当单击图像时,它会模糊并在其顶部显示播放和其他选项。我正在使用外部库 (https://github.com/daimajia/AndroidViewHover) 来实现这种特殊效果,但我的问题本质上是通用的。
问题陈述:-
我正在处理模糊表面显示的播放选项上的点击事件,即适配器中的 hover_sample.xml (TrendingAdapter.java),我想调用播放 youtube 视频的函数 (public void openWebView(TrendingData image)
) 在片段 (TrendingFragment.java) 中。所以,基本上我想从它的适配器访问片段中的函数。
我尝试了各种方法,但都不起作用。代码示例如下。
TrendingFragment.java:-
public class TrendingFragment extends Fragment implements AdapterView.OnItemClickListener, AdapterCallback
private GridView mGridView;
private TrendingAdapter mAdapter;
private String movieJson;
private String caption;
Activity MyActivity = getActivity();
public static TrendingFragment getInstance()
TrendingFragment fragment = new TrendingFragment();
return fragment;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
return inflater.inflate( R.layout.activity_trending_fragment, container, false );
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
mGridView = (GridView) view.findViewById( R.id.grid );
mGridView.setOnItemClickListener( this );
mGridView.setDrawSelectorOnTop( true );
TrendingActivity activity = (TrendingActivity) getActivity();
movieJson = activity.getMovieJson();
caption = activity.getNewCaption();
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
super.onActivityCreated(savedInstanceState);
mAdapter = new TrendingAdapter( getActivity(), 0, this);
mGridView.setAdapter(mAdapter);
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(movieJson)
.build();
TrendingApiInterface trendingApiInterface = restAdapter.create( TrendingApiInterface.class );
trendingApiInterface.getStreams(new Callback<List<TrendingData>>()
@Override
public void success(List<TrendingData> galleryImages, Response response)
if (galleryImages == null || galleryImages.isEmpty() || !isAdded() )
return;
for (TrendingData image : galleryImages)
mAdapter.add(image);
mAdapter.notifyDataSetChanged();
@Override
public void failure(RetrofitError error)
);
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
TrendingData image = (TrendingData) parent.getItemAtPosition( position );
openWebView(image);
public void openWebView(TrendingData image)
try
Intent intent = YouTubeStandalonePlayer.createVideoIntent(getActivity(), Config.YOUTUBE_API_KEY, image.getImage());
startActivity(intent);
//If Youtube app is not present then open in webview
catch (ActivityNotFoundException ex)
Toast.makeText(getActivity(), "Please wait for few minutes.Its Loading...", Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), "Latest Youtube Player is missing on your device.Opening in WebView.Download it for better experience", Toast.LENGTH_LONG).show();
String complete_url="https://www.youtube.com/watch?v=";
Intent intent = new Intent(getActivity(), webView.class );
intent.putExtra( webView.EXTRA_IMAGE, complete_url+image.getImage());
intent.putExtra( webView.PREVIOUS_ACTIVITY, "TrendingActivity.class");
intent.putExtra(webView.EXTRA_CAPTION, image.getCaption());
intent.putExtra( webView.EXTRA_BG_IMAGE, image.getBgImage());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//finish();
startActivity(intent);
@Override
public void onMethodCallback(AdapterView<?> parent, View view, int position, long id)
TrendingData image = (TrendingData) parent.getItemAtPosition( position );
openWebView(image);
TrendingAdapter.java:-
公共类 TrendingAdapter 扩展 ArrayAdapter
TrendingFragment lfg;
TrendingData image;
private AdapterCallback mAdapterCallback;
public TrendingAdapter(Context context, int resource, AdapterCallback callback)
super(context, resource);
this.mAdapterCallback = callback;
//hover image
private BlurLayout mSampleLayout ;
@Override
public View getView(final int position, View convertView, final ViewGroup parent)
final ViewHolder holder;
if( convertView == null )
holder = new ViewHolder();
convertView = LayoutInflater.from( getContext() ).inflate( R.layout.activity_trending_adapter, parent, false );
holder.image = (ImageView) convertView.findViewById( R.id.image );
holder.caption=(TextView) convertView.findViewById(R.id.textView);
//holder.progress = (ProgressBar) convertView.findViewById(R.id.progressBar);
convertView.setTag(holder);
BlurLayout.setGlobalDefaultDuration(450);
mSampleLayout = (BlurLayout)convertView.findViewById(R.id.blur_layout);
final View hover = LayoutInflater.from(getContext()).inflate(R.layout.hover_sample, null);
holder.play = (ImageView)hover.findViewById(R.id.heart);
holder.playList = (ImageView)hover.findViewById(R.id.share);
hover.findViewById(R.id.heart).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
YoYo.with(Techniques.Tada)
.duration(550)
.playOn(v);
);
hover.findViewById(R.id.share).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
YoYo.with(Techniques.Swing)
.duration(550)
.playOn(v);
);
mSampleLayout.setHoverView(hover);
mSampleLayout.setBlurDuration(550);
mSampleLayout.addChildAppearAnimator(hover, R.id.heart, Techniques.FlipInX, 550, 0);
mSampleLayout.addChildAppearAnimator(hover, R.id.share, Techniques.FlipInX, 550, 250);
mSampleLayout.addChildAppearAnimator(hover, R.id.more, Techniques.FlipInX, 550, 500);
mSampleLayout.addChildDisappearAnimator(hover, R.id.heart, Techniques.FlipOutX, 550, 500);
mSampleLayout.addChildDisappearAnimator(hover, R.id.share, Techniques.FlipOutX, 550, 250);
mSampleLayout.addChildDisappearAnimator(hover, R.id.more, Techniques.FlipOutX, 550, 0);
mSampleLayout.addChildAppearAnimator(hover, R.id.description, Techniques.FadeInUp);
mSampleLayout.addChildDisappearAnimator(hover, R.id.description, Techniques.FadeOutDown);
else
holder = (ViewHolder) convertView.getTag();
final ViewHolder tmp = holder;
Picasso.with(getContext()).load(getItem(position).getThumbnail())
.placeholder(getContext().getResources().getDrawable(R.drawable.place)).
error(getContext().getResources().getDrawable(R.drawable.place)).
into(holder.image);
return convertView;
private class ViewHolder
ImageView image;
ProgressBar progress;
TextView caption;
ImageView play;
ImageView playList;
hover_sample.xml:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<LinearLayout
android:id="@+id/animation_area"
android:layout_centerInParent="true"
android:layout_
android:orientation="horizontal"
android:gravity="center"
android:layout_>
<ImageView
android:layout_marginRight="20dp"
android:id="@+id/heart"
android:src="@drawable/heart"
android:layout_
android:layout_ />
<ImageView
android:id="@+id/share"
android:layout_marginRight="20dp"
android:src="@drawable/share"
android:layout_
android:layout_/>
<ImageView
android:id="@+id/more"
android:src="@drawable/more"
android:layout_
android:layout_/>
</LinearLayout>
<TextView
android:id="@+id/description"
android:text="Parse php SDK"
android:layout_marginLeft="12dp"
android:textColor="#ffffff"
android:layout_marginBottom="12dp"
android:layout_alignParentBottom="true"
android:layout_
android:layout_ />
</RelativeLayout>
请建议如何实现相同的目标。对于类似的问题,我尝试了以这种形式提出的各种方法,但没有任何成功。
【问题讨论】:
【参考方案1】:使用回调方法。创建一个接口。让你的片段实现它。在其构造函数中将回调传递给适配器。适配器使用回调来调用接口方法,您可以从中调用 webview()。
见How to create interface between fragment and adapter。
在从片段调用任何适配器的方法时,您可以使用适配器的实例,但从适配器到片段的通信应该通过回调来完成。
【讨论】:
这正是我正在尝试的,但我无法从适配器调用 webview,因为我需要提供参数“TrendingData image”以进行进一步操作。如何在适配器中获取 TrendingData。请参阅 TrendingFragment.java 中的 public void onMethodCallback () Raghu,您只需将其作为参数传递,别无他法。我想你做到了。但是你应该确保当你不再需要它时你对 bgImage 的引用是空的。以便GC可以收集。 还将答案标记为已接受。 .这让他们有信心在他们的代码中使用该解决方案。【参考方案2】:正如 cgr 在上面的帖子中所建议的,我创建了一个接口来在 Fragment 和它的适配器之间进行通信。关注 cgr 共享的链接。太棒了。
接口定义:-
void onMethodCallback(String url, String caption, String bgImage);
然后,使用适配器类中的回调函数为:-
mAdapterCallback.onMethodCallback(getItem(position).getImage(), getItem(position).getCaption(),getItem(position).getBgImage());
这解决了我的问题。现在,我可以成功地从它的片段类调用适配器类中的任何函数。
【讨论】:
以上是关于无法在其适配器 Android 应用开发中调用 Fragment 的功能的主要内容,如果未能解决你的问题,请参考以下文章
无法在 Android 12.0 中退出 Flutter 应用