我在我的片段中添加了一个预告片列表视图。当点击一个项目时,它应该开始播放视频的意图但由于某种原因,未检测到点击
<ScrollView
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"
android:fillViewport="true"
tools:context="com.example.user.popularmovies.ItemActivityFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:text="beauty and the beast"
android:id="@+id/movie_title"
android:padding="@dimen/tv_padding"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:visibility="visible"
android:layout_alignRight="@+id/empty_view"
android:layout_alignEnd="@+id/empty_view" />
<ImageView
android:layout_width="@dimen/iv_layout_width"
android:layout_height="@dimen/iv_layout_height"
android:layout_marginTop="@dimen/iv_layout_marginTop"
android:id="@+id/movie_poster"
android:layout_below="@id/movie_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:visibility="visible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:text="beauty and the beast"
android:id="@+id/movie_year"
android:visibility="visible"
android:layout_alignBaseline="@+id/movie_title"
android:layout_alignBottom="@+id/movie_title"
android:layout_alignRight="@+id/card_view"
android:layout_alignEnd="@+id/card_view" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:text="beauty and the beast"
android:id="@+id/movie_rating"
android:visibility="visible"
android:layout_below="@+id/movie_year"
android:layout_alignLeft="@+id/movie_year"
android:layout_alignStart="@+id/movie_year" />
<View
android:layout_width="fill_parent"
android:layout_height="@dimen/v_layout_height"
android:layout_margin="@dimen/v_layout_margin"
android:layout_below = "@id/movie_poster"
android:background="#ffffff"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/black_line"
android:visibility="visible" />
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignRight="@+id/black_line"
android:layout_alignEnd="@+id/black_line"
card_view:cardCornerRadius="@dimen/tv_cardCornerRadius"
android:layout_toRightOf="@+id/empty_view"
android:layout_toEndOf="@+id/empty_view"
android:layout_above="@+id/empty_view"
android:layout_alignTop="@+id/movie_poster">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/movie_overview"
android:visibility="visible"/>
</android.support.v7.widget.CardView>
<ListView
android:fastScrollEnabled="false"
android:id="@+id/list"
android:layout_below="@+id/movie_poster"
android:listSelector="@android:color/transparent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp"
android:layout_toLeftOf="@+id/empty_view"
android:layout_toStartOf="@+id/empty_view" />
<!-- Empty view is only visible when the list has no items. -->
<TextView
android:id="@+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="?android:textAppearanceMedium"/>
</RelativeLayout>
</ScrollView>
这是我的onCreateView代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item, container,
false);
WindowManager wm = (WindowManager)
rootView.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
initComponents(rootView);
setValues(rootView);
// Find a reference to the {@link ListView} in the layout
ListView trailerListView = (ListView)
rootView.findViewById(R.id.list);
mEmptyStateTextView = (TextView)
rootView.findViewById(R.id.empty_view);
trailerListView.setEmptyView(mEmptyStateTextView);
//Create a new adapter that takes an empty list of earthquakes as
input
mAdapter = new TrailerAdapter(getActivity(), new ArrayList<Trailer>());
// Set the adapter on the {@link ListView}
// so the list can be populated in the user interface
trailerListView.setAdapter(mAdapter);
// Set an item click listener on the ListView, which sends an intent to a web browser
// to open a website with more information about the selected earthquake.
// Get a reference to the ConnectivityManager to check state of network connectivity
ConnectivityManager connMgr = (ConnectivityManager)
rootView.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
// Get details on the currently active default data network
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
// If there is a network connection, fetch data
if (networkInfo != null && networkInfo.isConnected()) {
// Get a reference to the LoaderManager, in order to interact with loaders.
android.support.v4.app.LoaderManager loaderManager = getLoaderManager();
// Initialize the loader. Pass in the int ID constant defined above and pass in null for
// the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
// because this activity implements the LoaderCallbacks interface).
loaderManager.initLoader(TRAILER_LOADER_ID, null, this);
} else {
// Otherwise, display error
// First, hide loading indicator so error message will be visible
// View loadingIndicator = findViewById(R.id.loading_indicator);
// loadingIndicator.setVisibility(View.GONE);
// Update empty state with no connection error message
mEmptyStateTextView.setText(R.string.no_internet_connection);
}
trailerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Find the current trailer that was clicked on
Trailer currentTrailer = mAdapter.getItem(position);
String trailerLink = "https://www.youtube.com/watch?v=" + currentTrailer.getKey();
// Convert the String URL into a URI object (to pass into the Intent constructor)
Uri trailerUri = Uri.parse(trailerLink);
//Toast.makeText(MainActivity.this ,currentTrailer.getKey() ,Toast.LENGTH_LONG).show();
// Create a new intent to view the trailer URI
Intent websiteIntent = new Intent(Intent.ACTION_VIEW, trailerUri);
// Send the intent to launch a new activity
startActivity(websiteIntent);
}
});
return rootView;
}
适配器代码
public class TrailerAdapter extends ArrayAdapter<Trailer> {
public TrailerAdapter(@NonNull Context context, @NonNull List<Trailer> trailers) {
super(context, 0, trailers);
}
/* public TrailerAdapter(ItemActivity.PlaceholderFragment context,
List<Trailer> trailers) {
super(context, 0, trailers);
}*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if there is an existing list item view (called convertView) that we can reuse,
// otherwise, if convertView is null, then inflate a new list item layout.
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.trailer_list_item, parent, false);
}
// Find the earthquake at the given position in the list of trailers
Trailer currentTrailer = getItem(position);
// Find the TextView with view ID magnitude
TextView nameView = (TextView) listItemView.findViewById(R.id.name);
// Display the magnitude of the current earthquake in that TextView
nameView.setText(currentTrailer.getName());
// Return the list item view that is now showing the appropriate data
return listItemView;
}
@Override
public boolean isEnabled(int position) {
return false;
}
@Override
public boolean areAllItemsEnabled() {
return super.areAllItemsEnabled();
}
}
片段代码
public static class PlaceholderFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<Trailer>>{
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item, container, false);
WindowManager wm = (WindowManager) rootView.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
initComponents(rootView);
setValues(rootView);
// Find a reference to the {@link ListView} in the layout
ListView trailerListView = (ListView) rootView.findViewById(R.id.list);
mEmptyStateTextView = (TextView) rootView.findViewById(R.id.empty_view);
trailerListView.setEmptyView(mEmptyStateTextView);
//Create a new adapter that takes an empty list of earthquakes as input
mAdapter = new TrailerAdapter(getActivity(), new ArrayList<Trailer>());
// Set the adapter on the {@link ListView}
// so the list can be populated in the user interface
trailerListView.setAdapter(mAdapter);
// Set an item click listener on the ListView, which sends an intent to a web browser
// to open a website with more information about the selected earthquake.
// Get a reference to the ConnectivityManager to check state of network connectivity
ConnectivityManager connMgr = (ConnectivityManager)
rootView.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
// Get details on the currently active default data network
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
// If there is a network connection, fetch data
if (networkInfo != null && networkInfo.isConnected()) {
// Get a reference to the LoaderManager, in order to interact with loaders.
android.support.v4.app.LoaderManager loaderManager = getLoaderManager();
// Initialize the loader. Pass in the int ID constant defined above and pass in null for
// the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
// because this activity implements the LoaderCallbacks interface).
loaderManager.initLoader(TRAILER_LOADER_ID, null, this);
} else {
// Otherwise, display error
// First, hide loading indicator so error message will be visible
// View loadingIndicator = findViewById(R.id.loading_indicator);
// loadingIndicator.setVisibility(View.GONE);
// Update empty state with no connection error message
mEmptyStateTextView.setText(R.string.no_internet_connection);
}
trailerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Find the current trailer that was clicked on
Trailer currentTrailer = mAdapter.getItem(position);
String trailerLink = "https://www.youtube.com/watch?v=" + currentTrailer.getKey();
// Convert the String URL into a URI object (to pass into the Intent constructor)
Uri trailerUri = Uri.parse(trailerLink);
//Toast.makeText(MainActivity.this ,currentTrailer.getKey() ,Toast.LENGTH_LONG).show();
// Create a new intent to view the trailer URI
Intent websiteIntent = new Intent(Intent.ACTION_VIEW, trailerUri);
// Send the intent to launch a new activity
startActivity(websiteIntent);
}
});
return rootView;
}
public void initComponents(View rootView){
movie = new Movie();
ItemActivity.intent = getActivity().getIntent();
int movie_id = intent.getIntExtra("movie_id",0);
int movie_position = intent.getIntExtra("movie_position",0);
movie = MainActivity.moviesList.get(movie_position);
movie_title = (TextView)rootView.findViewById(R.id.movie_title);
movie_year = (TextView)rootView.findViewById(R.id.movie_year);
movie_rate = (TextView)rootView.findViewById(R.id.movie_rating);
movie_poster = (ImageView)rootView.findViewById(R.id.movie_poster);
movie_overview = (TextView)rootView.findViewById(R.id.movie_overview);
}
public static void setValues(View rootView){
movie_title.setText(movie.getOriginal_title());
movie_year.setText( movie.getRelease_date().substring(0,4));
movie_title.setVisibility(View.VISIBLE);
movie_rate.setText(movie.getVote_average() + "/10");
movie_overview.setText(movie.getOverview());
String movie_poster_url;
if (movie.getPoster_path() == TMDB_API.IMAGE_NOT_FOUND) {
movie_poster_url = TMDB_API.IMAGE_NOT_FOUND;
}else {
movie_poster_url = TMDB_API.IMAGE_URL + TMDB_API.IMAGE_SIZE_185 + "/" + movie.getPoster_path();
}
Picasso.with(rootView.getContext()).load(movie_poster_url).into(movie_poster);
movie_poster.setVisibility(View.VISIBLE);
}
@Override
public Loader<List<Trailer>> onCreateLoader(int id, Bundle args) {
Uri baseUri = Uri.parse(REQUEST_URL);
Uri.Builder uriBuilder = baseUri.buildUpon();
return new TrailerLoader(getActivity(), uriBuilder.toString());
}
@Override
public void onLoadFinished(Loader<List<Trailer>> loader, List<Trailer> trailers) {
/// Hide loading indicator because the data has been loaded
// View loadingIndicator = findViewById(R.id.loading_indicator);
// loadingIndicator.setVisibility(View.GONE);
// Set empty state text to display "No trailers found."
mEmptyStateTextView.setText(R.string.no_trailers);
// Clear the adapter of previous earthquake data
mAdapter.clear();
// If there is a valid list of {@link Earthquake}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (trailers != null && !trailers.isEmpty()) {
mAdapter.addAll(trailers);
}
}
@Override
public void onLoaderReset(Loader<List<Trailer>> loader) {
// Loader reset, so we can clear out our existing data.
mAdapter.clear();
}
}
我试图添加android:clickable =&#34; false&#34; 机器人:可聚焦=&#34;假&#34;&GT;通常建议的XML,但没有工作
答案 0 :(得分:0)
有类似的问题。通过在列表项XML中的RelativeLayout中添加以下内容来解决该问题:
android:descendantFocusability="blocksDescendants"
此外,如果这不起作用,请尝试完全摆脱ScrollView。