我正在创建一个Android应用程序,当点击图像时,它会模糊并显示游戏和其他顶部的选项。我正在使用外部库(https://github.com/daimajia/AndroidViewHover)来实现这种特殊效果,但我的问题本质上是通用的。
问题陈述: -
我在Blur表面显示的播放选项上处理click事件,即适配器中的hover_sample.xml( TrendingAdapter.java ),我想调用播放youtube视频的函数({{1在片段( TrendingFragment.java )中。所以,基本上我想从它的适配器访问片段中的函数。
我尝试了各种方法,但它不起作用。代码示例如下。
public void openWebView(TrendingData image)
TrendingAdapter.java: -
公共类TrendingAdapter扩展了ArrayAdapter {
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);
}
}
}
hover_sample.xml: -
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;
}
请建议如何实现同样的目标。我已经尝试过以这种形式提出的各种方式来解决类似问题但没有任何成功。
答案 0 :(得分:1)
使用回调方法。创建一个界面。让你的片段实现它。将回调传递给它的构造函数中的适配器。 Adapter使用回调来调用接口方法,您可以从中调用webview()。
见How to create interface between fragment and adapter。
从fragment调用任何适配器的方法时,可以使用适配器的实例,但是从适配器到片段的通信应该使用回调来完成。
答案 1 :(得分:0)
正如上面帖子中的cgr所建议的,我创建了一个在Fragment&amp; amp;之间进行通信的界面。它的适配器。关注cgr共享的链接。真棒。
界面定义: -
void onMethodCallback(String url, String caption, String bgImage);
然后,使用适配器类中的回调函数: -
mAdapterCallback.onMethodCallback(getItem(position).getImage(), getItem(position).getCaption(),getItem(position).getBgImage());
这解决了我的问题。现在,我成功地能够从它的片段类中调用适配器类中的任何函数。