我试着想出了第一个主意,但却犯了很多错误。我可以显示数据,但它会导致其他项移动并破坏顺序。结果,我想的这个主意不起作用。如果我的工作是;将我下载的电影排序到一个看不见的回收视图中。
我从jsonarray获取数据。我想在searchview建议列表中显示此数据。把它想象成在谷歌上搜索。我该怎么办?这是我的主要活动
`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeComponent();
LinearLayoutManager linearLayoutManagerParentRecyclerView = new LinearLayoutManager(this);
linearLayoutManagerParentRecyclerView.setOrientation(LinearLayoutManager.VERTICAL);
MovieInitializeDownLoader movieInitializeDownLoader =new MovieInitializeDownLoader(MainActivity.this,recyclerViewParent
, linearLayoutManagerParentRecyclerView,totalCategory,popular,topRate,upComing,nowPlaying);
movieInitializeDownLoader.download(JSON_POPULAR_LIST_URL,getString(R.string.popular));
movieInitializeDownLoader.download(JSON_TOP_RATE_LIST_URl,getString(R.string.Top_Rate));
movieInitializeDownLoader.download(JSON_NOW_PLAYING_LIST_URL,getString(R.string.Up_coming));
movieInitializeDownLoader.download(JSON_UP_COMING_LIST_URL,getString(R.string.now_playing));
sliderImageDownload(JSON_UP_COMING_LIST_URL);
if (getIntent().getBooleanExtra("LOGOUT", false))
{
finish();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.bottom_nav_menu,menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_home:
TxtFileReader txtFileReader=new TxtFileReader();
Intent intent=new Intent(MainActivity.this,UserMovieTopListActivity.class);
intent.putStringArrayListExtra(ON_CLICKED_TOP_LIST_BUTTON_MOVIE_ID_INTENT_KEY,txtFileReader.read());
startActivity( intent);
return true;
case R.id.action_search:
return true;
}
return super.onOptionsItemSelected(item);
}
public void sliderImageDownload(String imageDataURL){
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET
, imageDataURL
, null
, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray=response.getJSONArray(JSON_OBJECT_KEYWORD_RESULT);
for (int i=0;i<13;i++){
movie=new Movie();
movie.setId(jsonArray.getJSONObject(i).getInt(JSON_OBJECT_KEYWORD_ID));
movie.setMovieName(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_MOVIE_TITLE));
movie.setMoviePosterImageURL(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_POSTER_PATH));
movie.setRank(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_VOTE_AVERAGE));
movie.setReleaseDate(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_RELEASE_DATE));
movie.setMovieBackdropPathImageUrl(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_BACKDROP_PATH));
sliderViewImageArrayList.add(movie);
}
Log.d(TAG, "onResponse: "+sliderViewImageArrayList);
SliderImageAdapter sliderImageAdapter=new SliderImageAdapter(sliderViewImageArrayList,MainActivity.this);
sliderView.setSliderAdapter(sliderImageAdapter);
sliderView.startAutoCycle();
sliderView.setSliderTransformAnimation(SliderAnimations.CUBEINROTATIONTRANSFORMATION);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse: "+error);
}
});
HttpConnector.getInstance(MainActivity.this).addRequestQue(jsonObjectRequest);
}
public void initializeComponent(){
sliderView=findViewById(R.id.imageSlider);
recyclerViewParent=findViewById(R.id.recyclerViewParent);
searchRecyclerView=findViewById(R.id.searchRecyclerView);
}
@Override
public boolean onQueryTextSubmit(String query) {
searchRecyclerView.setVisibility(View.INVISIBLE);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET
,String.format(JSON_SEARCH_MOVIE_URL,newText)
, null
, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ArrayList<Movie> searchMovies=new ArrayList<>();
try {
JSONArray jsonArray=response.getJSONArray(JSON_OBJECT_KEYWORD_RESULT);
for (int i = 0; i < jsonArray.length(); i++) {
movie=new Movie();
movie.setId(jsonArray.getJSONObject(i).getInt(JSON_OBJECT_KEYWORD_ID));
movie.setMovieName(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_NAME));
searchMovies.add(movie);
}
if (!newText.isEmpty()){
RecyclerViewSearchViewAdapter searchViewAdapter=new RecyclerViewSearchViewAdapter(searchMovies);
LinearLayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
searchRecyclerView.setAdapter(searchViewAdapter);
searchRecyclerView.setLayoutManager(layoutManager);
searchRecyclerView.setVisibility(View.VISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse: "+error);
}
});
HttpConnector.getInstance(MainActivity.this).addRequestQue(jsonObjectRequest);
return false;
}`
这是我的主要布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
tools:context=".activity.MainActivity"
android:background="@color/darkBlue"
android:orientation="vertical"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/searchRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:background="@color/white"
/>
<com.smarteist.autoimageslider.SliderView
android:id="@+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="5dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:sliderAnimationDuration="600"
app:sliderAutoCycleDirection="back_and_forth"
app:sliderAutoCycleEnabled="true"
app:sliderIndicatorAnimationDuration="600"
app:sliderIndicatorGravity="center_horizontal|bottom"
app:sliderIndicatorMargin="15dp"
app:sliderIndicatorOrientation="horizontal"
app:sliderIndicatorPadding="3dp"
app:sliderIndicatorRadius="2dp"
app:sliderIndicatorSelectedColor="#5A5A5A"
app:sliderIndicatorUnselectedColor="#FFF"
app:sliderScrollTimeInSec="3"
app:sliderStartAutoCycle="true">
</com.smarteist.autoimageslider.SliderView>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerViewParent"
/>
这是我的菜单xml:
<item
android:id="@+id/action_search"
android:icon="@drawable/search_icon"
android:title="@string/search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"
/>
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home"
app:showAsAction="always"
/>
暂无答案!
目前还没有任何答案,快来回答吧!