gpt4 book ai didi

java - 无法在其Adapter中调用Fragment的功能,Android应用程序开发

转载 作者:行者123 更新时间:2023-12-01 10:49:55 26 4
gpt4 key购买 nike

我正在制作一个Android应用程序,当单击图像时,它会模糊并在其顶部显示播放和其他选项。我正在使用外部库( https://github.com/daimajia/AndroidViewHover )来实现这种特殊效果,但我的问题本质上是通用的。

问题陈述:-

我正在处理模糊表面显示的播放选项上的点击事件,即适配器中的hover_sample.xml (TrendingAdapter.java),并且我想调用用于播放 youtube 视频的函数 ( public void openWebView(TrendingData image)) 位于 fragment (TrendingFragment.java) 中。所以,本质上我想从其适配器访问 fragment 中的函数。

我尝试过各种方法,但都不起作用。代码示例如下。

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:-

公共(public)类 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_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/animation_area"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="match_parent">
<ImageView
android:layout_marginRight="20dp"
android:id="@+id/heart"
android:src="@drawable/heart"
android:layout_width="50dp"
android:layout_height="50dp" />
<ImageView
android:id="@+id/share"
android:layout_marginRight="20dp"
android:src="@drawable/share"
android:layout_width="50dp"
android:layout_height="50dp"/>
<ImageView
android:id="@+id/more"
android:src="@drawable/more"
android:layout_width="50dp"
android:layout_height="50dp"/>
</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_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

请建议如何实现同样的目标。对于类似的问题,我尝试了此表单中建议的各种方法,但没有成功。

最佳答案

使用回调方法。创建一个界面。让你的 fragment 实现它。将回调传递给适配器的构造函数。适配器使用回调来调用接口(interface)方法,您可以从中调用 webview()。

参见How to create interface between fragment and adapter

从 fragment 调用任何适配器的方法时,您可以使用适配器的实例,但从适配器到 fragment 的通信应通过回调完成。

关于java - 无法在其Adapter中调用Fragment的功能,Android应用程序开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33977058/

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